From patchwork Tue May 30 13:57:53 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Nefedov X-Patchwork-Id: 9754661 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9BD14602B9 for ; Tue, 30 May 2017 14:12:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8DF7E2847A for ; Tue, 30 May 2017 14:12:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 82C9E2848D; Tue, 30 May 2017 14:12:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAD_ENC_HEADER,BAYES_00, DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id AB67C2847B for ; Tue, 30 May 2017 14:12:03 +0000 (UTC) Received: from localhost ([::1]:54054 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dFhsQ-0007M0-J2 for patchwork-qemu-devel@patchwork.kernel.org; Tue, 30 May 2017 10:12:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45882) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dFhfN-0004vH-94 for qemu-devel@nongnu.org; Tue, 30 May 2017 09:58:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dFhfI-0007DF-O6 for qemu-devel@nongnu.org; Tue, 30 May 2017 09:58:33 -0400 Received: from mail-he1eur01on0124.outbound.protection.outlook.com ([104.47.0.124]:10752 helo=EUR01-HE1-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dFhfI-0007Cn-G2 for qemu-devel@nongnu.org; Tue, 30 May 2017 09:58:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=8FOjow0tmbs4wGIM6cFqIRQHD8QvKYFO4Ttwe7pO2II=; b=hNUFgUBPp0h9XZKrb6mkvd3n2eUP8iITxlaV11s9qylzBTT6Ebll3/nLtfcwoIQz+Swl+xZNOUnKp3BbKYE9zbzq3f4eZlVkaWIubA6PYLMEWBM4J9xA43JdYLd0xj+Px/YvSZi4Lg7EN85Hj9DMGU0u04onVCcQlGamLmXTxe4= Authentication-Results: nongnu.org; dkim=none (message not signed) header.d=none; nongnu.org; dmarc=none action=none header.from=virtuozzo.com; Received: from xantnef-ws.sw.ru (195.214.232.6) by HE1PR0801MB1993.eurprd08.prod.outlook.com (2603:10a6:3:4f::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1124.9; Tue, 30 May 2017 13:58:26 +0000 From: Anton Nefedov To: Date: Tue, 30 May 2017 16:57:53 +0300 Message-ID: <1496152683-102751-4-git-send-email-anton.nefedov@virtuozzo.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1496152683-102751-1-git-send-email-anton.nefedov@virtuozzo.com> References: <1496152683-102751-1-git-send-email-anton.nefedov@virtuozzo.com> MIME-Version: 1.0 X-Originating-IP: [195.214.232.6] X-ClientProxiedBy: DB6PR02CA0033.eurprd02.prod.outlook.com (2603:10a6:6:15::46) To HE1PR0801MB1993.eurprd08.prod.outlook.com (2603:10a6:3:4f::23) X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: HE1PR0801MB1993: X-MS-Office365-Filtering-Correlation-Id: f1d5f5d7-45e7-4d27-974d-08d4a763f224 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(201703131423075)(201703031133081); SRVR:HE1PR0801MB1993; X-Microsoft-Exchange-Diagnostics: 1; HE1PR0801MB1993; 3:7olHNyp7GoG7FLhpPZveGtUoSEeKvfCkdQZuQXW/UJsiSX4JTcn5i3TmbvZHGJjCljFz+BFoBzh9t6lgCch0moUrJ7zct2yX9basTDpr1mVBAU+/t+C+gVNbsGw+G9Iypda1ZPEMwRVf0bkE7hsARpjRIhQBsrK04j8hjCwgWUZojW1A/1N8hi8kQHMtN10PureeQIl7iFdBwsvEsH8xjPkFepTJEYFk+l+x1Y/qhQ04MZN9GB8ve0j6bB9WOtXhccIKIMxfUa7AFm9BI/yqIEpG6qn+kwHmhuABTtJ8BOVzQfD6d0zL0iaf7/fj5x16GyTxqWkOHLxf4+dB8NLNwQ==; 25:nrfQbzCjhrHfAohO8WFRQ+7X3wo6zKMeEJdMTwhuoc0e6/otMH07b9qOSgdgPOaMrZ7JhjFqYoSBCZ6b2nkA4mpGSRleDXrwnnG3icySUCi7E3xc+tr/7z4TiuZGq9t1iYpg6YGokVhCze3XDNLrqoNgcOoQ9Q4kd/71OWwnaElFSxWkDwrUR8z07ph/fyqEhY8Xth+lnyHneZFPq1zGKTOk+/pupcQHbTJz7KywVO7VGRGy22ol0Ia2vpFoOP8yaZjiaDMbB3n3TogP6uXczNq+90bbdRePGDisG9lwU4rKSdMSq4aMhFpx47ZNfcnsNTmQzLrp+qSwKNGzfMxZ/8sqpoK7kmSKLwHXfw2Hjmfxci9+e9uXXhclkWc7pbRFmVPdPZ93KM5KyaUXZspFAB/8OzixV8EqGfMnXUad+jkqew9lZiXOWz+7wqeQdc/Yw/lKAXebVje1s8WQmQ9l4osmHgxssYAjntWL8mTV44c= X-Microsoft-Exchange-Diagnostics: 1; HE1PR0801MB1993; 31:YHTH74JMAtcdwKK0f0frzDW1luSj6D3rc5yyytkSVmhjITngMISWrQ89/setZzRHK0RxbIYdNedjuPZfAZRmiZmp8jXUhQs4cu1hRShBGHqIr85NR1McuMDDeehuV7cGhRWUOHQcWDFnnpaOS/euzabDIfS6F1RVfBDjlRpdbSFj6/kAMOq6WPuEqGJ+plFGMPf8CA9/ohE8BcKmOatYeQNB23d5mD2aJ8qQ97oJokkkTRd0SQFpJXWpfNavpsyY83vuToorhvrRJuWo908Bvg==; 20:9iqGIVr7WfEbE5pw+r7x9W8Mpjq1c4EkFlX9X5pUdLe3rEp2IKQ2DSCGrr/F7yZfGcqn1IHVKJhktsheQZIkXuqlbbPVy+WFJv9Q2w+O8wD2VilGH4cVHLvx77c+L0L6J4AKrTZ48AEinSsnEQUVJZnJZu5zuuyLI+sSCtU2zMTK7REoTyAiTscrB6zIqPaWaRq06yZGpO8l7Rjm0K6sxSee/2VytgA+6xFLIKfJ2s0kHDBFhRnFgVpz8HlbW/h91cz/vktS8hkk/hQztTxrVJe3lI3RcbDjNJjc5EC+yHXCG5dB1m7TC9K9Lv6ls9UvFIEckM57acJ7JFRlmDyi8pIB1KHI8gvxFD+2IMxV/V0g78rH3YVGwRfcnwtIKbkQbv8WUNfa0aeyhrXRAtIBCJTaDdzeQ6g+q+O7NpoBd5k= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(158342451672863); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(100000700073)(100105000095)(100000701073)(100105300095)(100000702073)(100105100095)(6040450)(601004)(2401047)(8121501046)(5005006)(93006095)(93001095)(3002001)(100000703073)(100105400095)(10201501046)(6041248)(20161123564025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123560025)(20161123562025)(20161123558100)(20161123555025)(6072148)(100000704073)(100105200095)(100000705073)(100105500095); SRVR:HE1PR0801MB1993; BCL:0; PCL:0; RULEID:(100000800073)(100110000095)(100000801073)(100110300095)(100000802073)(100110100095)(100000803073)(100110400095)(100000804073)(100110200095)(100000805073)(100110500095); SRVR:HE1PR0801MB1993; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; HE1PR0801MB1993; 4:Vx4fYDnB706UM1R6YFq3mtW7iTMV6moj86JunKQ5?= =?us-ascii?Q?6+Banl3eiUBFSh0vz7vCXM20/BoZIxF5clV/BlROV52L667POyMUFJ2pb4zS?= =?us-ascii?Q?QkjAMX1of39C07S04mwvoPOiz1oC5Qw23M34U1JE4RFFipozJ8q+S+3Jdmok?= =?us-ascii?Q?LbPhLnoraUxt5Fw2VAOpPqEqn2aIR8ZUBbadptXmBmtWpjxRhKEeYPFdRKLN?= =?us-ascii?Q?6gIARMA23619pJ3UaWQZDuLf5EG5utcHUVUEVoke2jwKrcc1HvKo6kZO6WfZ?= =?us-ascii?Q?qP5kS0PmfXzVxNDhAiKxIuZu6MNGggEmD6pBQ/GScoo8SB3pspXoRFhsrqio?= =?us-ascii?Q?T5kF1VlCjrwQj+lXk7HAV6mCEL0GQEKFWCfmwQ7sbkgwTgSDMofyGo3SE/NB?= =?us-ascii?Q?Q+FbfRXcVnsoMSInNW5toZ///9AKrLW/w+vjhC7SlC0Ysnuj3TiHmSNgDgwZ?= =?us-ascii?Q?0Gd2N7sRLauESnD/FfcAr/jLhxwX06msq6egMQF12ub4OcmWvU7mmQe6t4FG?= =?us-ascii?Q?9mowxZVsMP2BZIj8nAqSiBj9ZERIAJazmEKOk++zBRZUaIsDLoLk9O+rTXMN?= =?us-ascii?Q?KlxlK0zU7riux3R0yCQ3mAdLy3tqXyEr4uDiDtc/HqQzKT3kG6LB2dbJoUSF?= =?us-ascii?Q?DuWlM+cu+878NyYtwOwj36N5Rk8ofcI2SUwSFlXAEc9a18n2l7tPGQi0dzSK?= =?us-ascii?Q?U4pCXe+mwe62WPHIuHHquUAcBQYbdglNpgz9WgQydWDTK0rIAOBPWVQ9zekS?= =?us-ascii?Q?uLnSQnUJX+6XVlDJnpLx7gl0gz/N14ITudcOVMJ6GNIMbDU5kMB8IHZrO5Tt?= =?us-ascii?Q?wx6f/IJnlolGmQHSPbriwc1EzPRgBQDdXRlIDtGhhVIBXfL6HMULedJ/8j45?= =?us-ascii?Q?0IHiDwGrePOqjun8qYF7OvYNgZmUCIu2Dc6ev1lsbDnIzqCgRNE0na+az5yM?= =?us-ascii?Q?HLtoSkcHjOsTkP8ips2BzRDE82i4gHvb0BmzN+/1VHosE75clbmj0yrkocUI?= =?us-ascii?Q?bQTNYkt7o95zATzhJ4N055tZQdEe5IUlX3L9V9XrYISbwpjyAP8H5KDdyinc?= =?us-ascii?Q?FoR6u62wG8ZRtXSg55Fhr/L5LHoXcXriio6vKeY5ye+RARXoGdsFYNFxfqYS?= =?us-ascii?Q?CW34aqbBYQvMbBx6tvk7s7GSgxzADPZkYTsZKAyhamAT164zFXwD5X96rYDm?= =?us-ascii?Q?WJUKp2ROUovCWo4=3D?= X-Forefront-PRVS: 032334F434 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4630300001)(6009001)(39830400002)(39450400003)(39410400002)(39400400002)(6506006)(6486002)(86362001)(575784001)(6916009)(2950100002)(6666003)(54906002)(38730400002)(53936002)(6512007)(25786009)(478600001)(5660300001)(47776003)(305945005)(7736002)(66066001)(107886003)(5003940100001)(110136004)(189998001)(2906002)(4326008)(50226002)(3846002)(8676002)(81166006)(6116002)(53416004)(50466002)(2351001)(48376002)(76176999)(42186005)(50986999)(33646002)(36756003); DIR:OUT; SFP:1102; SCL:1; SRVR:HE1PR0801MB1993; H:xantnef-ws.sw.ru; FPR:; SPF:None; MLV:sfv; LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; HE1PR0801MB1993; 23:E/DT7gSyeTIW14xLVAEHEbtHmL6S2Do+H+2Tf7B?= =?us-ascii?Q?8/7fOIH82VUto8t/Ls+cLUnhNJZ2cPmOy+xHBjdfezuygxH6nHzcJ1G7nPhh?= =?us-ascii?Q?dEHdE4MlvcFNuQomtkS0e/tcEXT8S2BAoTltdGhZuVSrh+tv/UoIjxixkzTd?= =?us-ascii?Q?8fLGd09/uCsksPr7QeHBQ48biJTQcMJcDg12b42F70ZYx+mvw5+u2rpeCTkh?= =?us-ascii?Q?iz7jaT00Cmvd1735bi1eJMFYUhBBB8vBcCGzl4gR8fMx9m+24b79UpIOY9vB?= =?us-ascii?Q?oApMxfYzaBZ9FH3Y5Y9BTvi3zw11d4gcFTDe/hSsLMLp4evv7I3IkbLxFdts?= =?us-ascii?Q?O62j/44nPIdHC4jfNCwUj6+TERopHIZCuQd8lDIgvdXZhNh6FN3cLybIRXyR?= =?us-ascii?Q?+IpRNEBu8gGZldJVdRnTQpPEmf+joeP6df0ZA7+UP762yfLJhxw66o190QeL?= =?us-ascii?Q?7L3fOYzgDEA2mAQkQtrUZXVgMO/QRRKasEGGGOoKfABmP8C4pNLvitFZvSnM?= =?us-ascii?Q?HQSgSIuw11Khx4nnv9PMhpyP9fI1njunZA5xMvkRB/s+OQlHwt44ltOqTN2n?= =?us-ascii?Q?Y93Zu/vr8dcXWZm2LsQpS7vHVElo8h2HlkxrMlrtUI6EL26jFhNGVShBmg9v?= =?us-ascii?Q?7ocDcqGdVBURo5OZ7TCNnsomVcfLv5uRhpT/7o/IBVKEjpvOoKyECqp0Rewi?= =?us-ascii?Q?h6vpRtAnUaGGIZoBOBNJ19eJtOTiaREdpDHx9vIu65rS7Nmle6hIkqlEC1n+?= =?us-ascii?Q?gWJpi+lm1MiN5WbtqfLXX3fEjMYS2vOUytfIBkwjU5YCB+2avVoUYKBvXLrZ?= =?us-ascii?Q?fz3C/0ID/55JcDOVzRtzHWhsMmymkjsM86BNVganqNwLOm9jZGjBh9MEK1ty?= =?us-ascii?Q?FigxLIOme//Ul7cWmg/oNYB0vxCLwhSuGRV8EEDKUoa/x7+K79LjoJHugA9T?= =?us-ascii?Q?KtgUyGyT2wVYLNbrWGiNMFJ1xWcsJRsxu8bgl2zz57BCBla8E0OKCQq00Zxq?= =?us-ascii?Q?tJGFHbQvWsN0akioFMxjFisag18G7XBUFgmt6VPwkv/5kW65CmypQYGHnLgT?= =?us-ascii?Q?VreRulJwZywAfVmQNAZxDgxGqOT+qOjkPK6lEGWbkHmc6Pkrlrg=3D=3D?= X-Microsoft-Exchange-Diagnostics: 1; HE1PR0801MB1993; 6:4GqB9f/e7pLbVVKPZoFI1LKCagUKsgUrXHrsCzA0mwceDG/zq9eG0CD8DBJx6HvYXwqX04nKXfVFNKjTcOWdvevGRX+KqpCsWOVPSWPJU8Mz6POswwgFQXpCgs3kIYv7vmzQChg9Lv1d4sQgsnDQjdv+2un9Dk1Q597mnLmkUgHYI6BVzvbJWhshoWPWalsN02890QshxB06n+oASPxlU4dVA68HvvIcRQalS6ivLWHUiZ1q27HdycAVAEb5N2Kvp8fuUiec8ylhipTcWLacdolrcF9VZma8v0Vd5Bv6eU1gkfp5cfiw4e1HlgM8u61ahExYuuU+HOj4Lp44wGQ9ujtw3a+i5rwTdC4gl969V5NNuCRg+/FH+XIueuFhWSib6MVBdnF+JLCCDCUqFOcF4QEyOCPkShyy8yWMeBy25Gw4YRcy+h3FrciEVKxlCoeyqShgkQoZUmjASv9kIWrMudI1bKB+HrtENeHTedQDzh2IWJZkq6itZOqFFK5j8RJG/o/16see86cWAcMgtHYQiw== X-Microsoft-Exchange-Diagnostics: 1; HE1PR0801MB1993; 5:Clppxi349he/OG4Rx5YGpqamQlhUC/U3yRndCeDReeZ4UmTaDHRap7+XIhJr6eT1iIvvL6zLyN8U87jJNsgR0qZtgvSljFoh+qowbBivDkRuBkO0FXaZAZ31C0EPRvm2qmuUTxdpazje6cJx94lRWIG/Moks9aNxFQjO2EHcAKpk7OkV2LfLVt/U8oAXvylzh2eWnzY481h7T1rjhIK5l4wXghMK4pERkZXQz42/pq1pmJZdSFUygWPLApAsudEoyfhcKWtln9rLZduCxrDZv7fvMy7gWA0nK6rqWjRVq7lo1Dxw3gKVqSnvR2dgkmn9nhdUfucs2YmiRZe26Pj7oUU+QKGFqu/eqIgATs3sjuD42tJh5+WVENvW6Ox+pdrlOC+JrY2MD0rfFCQ4SBC3fnOc8S/sI00pISiXE3Go/q7oe/vnjyg1nlYJdxI8qAHIQ4sZ09q65QbJjazh+nDyA3Prqu8ZjwEeb7fTHp2N6IpMxDfvovGeGkGcXfhnOkqt; 24:0PAcB+/Oq+JYgIt/TQzgTFDgQTNLVeMHWFewTCMlUNuRXArMdnDmJbGScqMu7xn2YUHf9SiEvR2Jtqm9gmn5RXHdd+OG3zdUhn440l7+h3g= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; HE1PR0801MB1993; 7:VnooyvHLhSZFcURxGkJvRpxzMy8yd0sNvhdWH5Jx6wjadS95dDkdq5b0T2Q51rplHJPtnVU71xWbwYMIqvRRVA2G+Q87wto+YTmbPXl/THFSzqToaVztWZS7t8Z7F0JVqfjeXtBSzninP+9UIrqQZMSTV+6IEUXLBanhyKOgzMzvjc9k5nzgeLJG/QN41r9uqYy4UeNuYkM0NDEghNV6X/RolMx3FaxoefeYdmQ2F5jAhjdZSToO7Cc/9MRK+Wssgh2uSWjNFbLXo6A6NIiPgSdDPCky0619/NQpK5y5URmGNbB712IsxQ0+Qm6n3o50qPaoL+RY1627GJKcJVgj+w==; 20:pZkAX7e+Cry+uGk0FOu/AaEEs7r8Id2wtHY+gMUxVhX3Qjc71batefzLpawtrrfqxWttpSZo70MLTj6s86CTNcU85XKxyVIzRfs6es6M8cD1b3/U2e+IVbgUIBBTLxxTMehnwy9Aco4XaS7p2ZA6SsEFRkyfVeMEH9MlkWn/+jI= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 May 2017 13:58:26.4401 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0801MB1993 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.0.124 Subject: [Qemu-devel] [PATCH v3 03/13] char: chardevice hotswap X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, Anton Nefedov , den@virtuozzo.com, marcandre.lureau@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This patch adds a possibility to change a char device without a frontend removal. 1. Ideally, it would have to happen transparently to a frontend, i.e. frontend would continue its regular operation. However, backends are not stateless and are set up by the frontends via qemu_chr_fe_<> functions, and it's not (generally) possible to replay that setup entirely in a backend code, as different chardevs respond to the setup calls differently, so do frontends work differently basing on those setup responses. Moreover, some frontend can generally get and save the backend pointer (qemu_chr_fe_get_driver()), and it will become invalid after backend change. So, a frontend which would like to support chardev hotswap has to register a "backend change" handler, and redo its backend setup there. 2. Write path can be used by multiple threads and thus protected with chr_write_lock. So hotswap also has to be protected so write functions won't access a backend being replaced. Signed-off-by: Anton Nefedov Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Anton Nefedov --- chardev/char.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++---- include/sysemu/char.h | 9 ++++ qapi-schema.json | 40 ++++++++++++++++ 3 files changed, 165 insertions(+), 10 deletions(-) diff --git a/chardev/char.c b/chardev/char.c index 1b097b3..ba1a5f5 100644 --- a/chardev/char.c +++ b/chardev/char.c @@ -132,12 +132,16 @@ static bool qemu_chr_replay(Chardev *chr) int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len) { - Chardev *s = be->chr; + Chardev *s; ChardevClass *cc; int ret; + qemu_mutex_lock(&be->chr_lock); + s = be->chr; + if (!s) { - return 0; + ret = 0; + goto end; } if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) { @@ -145,7 +149,7 @@ int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len) replay_char_write_event_load(&ret, &offset); assert(offset <= len); qemu_chr_fe_write_buffer(s, buf, offset, &offset); - return ret; + goto end; } cc = CHARDEV_GET_CLASS(s); @@ -161,7 +165,9 @@ int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len) if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { replay_char_write_event_save(ret, ret < 0 ? 0 : ret); } - + +end: + qemu_mutex_unlock(&be->chr_lock); return ret; } @@ -191,13 +197,16 @@ int qemu_chr_write_all(Chardev *s, const uint8_t *buf, int len) int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len) { - Chardev *s = be->chr; + Chardev *s; + int ret; - if (!s) { - return 0; - } + qemu_mutex_lock(&be->chr_lock); + + s = be->chr; + ret = s ? qemu_chr_write_all(s, buf, len) : 0; - return qemu_chr_write_all(s, buf, len); + qemu_mutex_unlock(&be->chr_lock); + return ret; } int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len) @@ -478,7 +487,7 @@ Chardev *qemu_chr_fe_get_driver(CharBackend *be) return be->chr; } -bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp) +static bool qemu_chr_fe_connect(CharBackend *b, Chardev *s, Error **errp) { int tag = 0; @@ -507,6 +516,16 @@ unavailable: return false; } +bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp) +{ + if (!qemu_chr_fe_connect(b, s, errp)) { + return false; + } + + qemu_mutex_init(&b->chr_lock); + return true; +} + static bool qemu_chr_is_busy(Chardev *s) { if (CHARDEV_IS_MUX(s)) { @@ -531,6 +550,7 @@ void qemu_chr_fe_deinit(CharBackend *b) d->backends[b->tag] = NULL; } b->chr = NULL; + qemu_mutex_destroy(&b->chr_lock); } } @@ -1306,6 +1326,92 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, return ret; } +ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend, + Error **errp) +{ + CharBackend *be; + const ChardevClass *cc; + Chardev *chr, *chr_new; + bool closed_sent = false; + ChardevReturn *ret; + + chr = qemu_chr_find(id); + if (!chr) { + error_setg(errp, "Chardev '%s' does not exist", id); + return NULL; + } + + if (CHARDEV_IS_MUX(chr)) { + error_setg(errp, "Mux device hotswap not supported yet"); + return NULL; + } + + if (qemu_chr_replay(chr)) { + error_setg(errp, + "Chardev '%s' cannot be changed in record/replay mode", id); + return NULL; + } + + be = chr->be; + if (!be) { + /* easy case */ + object_unparent(OBJECT(chr)); + return qmp_chardev_add(id, backend, errp); + } + + if (!be->chr_be_change) { + error_setg(errp, "Chardev user does not support chardev hotswap"); + return NULL; + } + + cc = char_get_class(ChardevBackendKind_lookup[backend->type], errp); + if (!cc) { + return NULL; + } + + chr_new = qemu_chardev_new(NULL, object_class_get_name(OBJECT_CLASS(cc)), + backend, errp); + if (!chr_new) { + return NULL; + } + chr_new->label = g_strdup(id); + + if (chr->be_open && !chr_new->be_open) { + qemu_chr_be_event(chr, CHR_EVENT_CLOSED); + closed_sent = true; + } + + qemu_mutex_lock(&be->chr_lock); + chr->be = NULL; + qemu_chr_fe_connect(be, chr_new, &error_abort); + + if (be->chr_be_change(be->opaque) < 0) { + error_setg(errp, "Chardev '%s' change failed", chr_new->label); + chr_new->be = NULL; + qemu_chr_fe_connect(be, chr, &error_abort); + qemu_mutex_unlock(&be->chr_lock); + if (closed_sent) { + qemu_chr_be_event(chr, CHR_EVENT_OPENED); + } + object_unref(OBJECT(chr_new)); + return NULL; + } + qemu_mutex_unlock(&be->chr_lock); + + object_unparent(OBJECT(chr)); + object_property_add_child(get_chardevs_root(), chr_new->label, + OBJECT(chr_new), &error_abort); + object_unref(OBJECT(chr_new)); + + ret = g_new0(ChardevReturn, 1); + if (CHARDEV_IS_PTY(chr_new)) { + ret->pty = g_strdup(chr_new->filename + 4); + ret->has_pty = true; + } + + return ret; +} + void qmp_chardev_remove(const char *id, Error **errp) { Chardev *chr; diff --git a/include/sysemu/char.h b/include/sysemu/char.h index 9f8df07..014ebbc 100644 --- a/include/sysemu/char.h +++ b/include/sysemu/char.h @@ -92,6 +92,7 @@ typedef struct CharBackend { void *opaque; int tag; int fe_open; + QemuMutex chr_lock; } CharBackend; struct Chardev { @@ -141,6 +142,14 @@ void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend); */ Chardev *qemu_chr_new(const char *label, const char *filename); +/** + * @qemu_chr_change: + * + * Change an existing character backend + * + * @opts the new backend options + */ +void qemu_chr_change(QemuOpts *opts, Error **errp); /** * @qemu_chr_fe_disconnect: diff --git a/qapi-schema.json b/qapi-schema.json index e38c5f0..0f0df36 100644 --- a/qapi-schema.json +++ b/qapi-schema.json @@ -5097,6 +5097,46 @@ 'returns': 'ChardevReturn' } ## +# @chardev-change: +# +# Change a character device backend +# +# @id: the chardev's ID, must exist +# @backend: new backend type and parameters +# +# Returns: ChardevReturn. +# +# Since: 2.10 +# +# Example: +# +# -> { "execute" : "chardev-change", +# "arguments" : { "id" : "baz", +# "backend" : { "type" : "pty", "data" : {} } } } +# <- { "return": { "pty" : "/dev/pty/42" } } +# +# -> {"execute" : "chardev-change", +# "arguments" : { +# "id" : "charchannel2", +# "backend" : { +# "type" : "socket", +# "data" : { +# "addr" : { +# "type" : "unix" , +# "data" : { +# "path" : "/tmp/charchannel2.socket" +# } +# }, +# "server" : true, +# "wait" : false }}}} +# <- {"return": {}} +# +## +{ 'command': 'chardev-change', 'data': {'id' : 'str', + 'backend' : 'ChardevBackend' }, + 'returns': 'ChardevReturn' } + +## # @chardev-remove: # # Remove a character device backend