From patchwork Thu Feb 9 15:55:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmytro Semenets X-Patchwork-Id: 13134827 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.xenproject.org (lists.xenproject.org [192.237.175.120]) (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 E7BC2C61DA4 for ; Thu, 9 Feb 2023 15:56:47 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.492670.762325 (Exim 4.92) (envelope-from ) id 1pQ9Hj-0006uL-2X; Thu, 09 Feb 2023 15:56:31 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 492670.762325; Thu, 09 Feb 2023 15:56:31 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pQ9Hi-0006uE-Vj; Thu, 09 Feb 2023 15:56:30 +0000 Received: by outflank-mailman (input) for mailman id 492670; Thu, 09 Feb 2023 15:56:29 +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 1pQ9Hh-0006cl-H3 for xen-devel@lists.xenproject.org; Thu, 09 Feb 2023 15:56:29 +0000 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [2a00:1450:4864:20::62c]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 4fe65f33-a892-11ed-93b5-47a8fe42b414; Thu, 09 Feb 2023 16:56:27 +0100 (CET) Received: by mail-ej1-x62c.google.com with SMTP id p26so7633950ejx.13 for ; Thu, 09 Feb 2023 07:56:27 -0800 (PST) Received: from dsemenets-HP-EliteBook-850-G8-Notebook-PC.. ([91.219.254.72]) by smtp.gmail.com with ESMTPSA id v1-20020a170906b00100b0087862f45a29sm1027594ejy.174.2023.02.09.07.56.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Feb 2023 07:56:26 -0800 (PST) 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 Sender: "Xen-devel" X-Inumbo-ID: 4fe65f33-a892-11ed-93b5-47a8fe42b414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TBqwkdTRqUwsY27EiP5YbesMgj7n7qTjOsQBFo8GYrE=; b=Otue27v9JKkHSwyRJM7inigaaJ/A+nbUdm6nZA67OVZlQxRV2xH6IO+UkKh40PN8xB gWeXV+CpZp2jp5QOYprBO4JngVU9bzeiYjYYXRQGzqdDyvTgWe3DH6EhBJvJ0nrGRLA8 jCZ8w3K7PCdkgSUPL1rWA6Zo8PHMZIxP6Vf7QPZNeqbjX/YMGqjAPzNRiOtDbEqZJthw XDwXJnzBVvintI3+0my827uVjYyy5wHDcB7KQhYJbFqE838Ol7ozyeqk8t2rIXh5dUr6 urhnHkBIV3BKesNt2F7IbEr5Sma4fNYyOoh9+/zbbLHzS9l5hn4TxT/KHXfbOvsU0Srx U+kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TBqwkdTRqUwsY27EiP5YbesMgj7n7qTjOsQBFo8GYrE=; b=xa5D7uPWlej6ux3cqXPXQnEMGEvVXoTVIwZdh334bjVdjCaKKuCkcwMv/pgz0pJj6Y aV5KHXSs2OsikVGGvA5za/pHEfoyHxbWaVzAmls4zpNQ18KT4wqMS+LrW50xIsUgDnFg w4+wtiHOJImX4gwgHui6sbk8kxmU2M0Zla/nlOOtZEuSBTpfo3KoA5/5MJCp1zAQWKpN MWzI7WOe2ZBvkgGDg+0wRysqKfm4gk6NtdJzudhjS7NZfDb2CvpXcYON+jqh2aOMLAST YaBIGAu41R//RdPpgWeikLFsLwS3RHBm6ts1Ks5cal8lYieZpu51ZMvZcRRLtZ0QSBdw 0gpA== X-Gm-Message-State: AO0yUKWQFNwjvhUlVq+3ZAgB3uPYPIlplJpJlGpchJiPzny19s+4Qjsr bbx1uvcjzwS1Pkgx9QbR81V7vt5VjbZBSQ== X-Google-Smtp-Source: AK7set/puazmZO3ubl7G7w9/AN0S6fj2NUEpt69AFT0wH2aw4Z2mQuXg8i+CRoMJLfRMK0hs7qYXjA== X-Received: by 2002:a17:906:7383:b0:88d:5f84:2756 with SMTP id f3-20020a170906738300b0088d5f842756mr10021510ejl.0.1675958187167; Thu, 09 Feb 2023 07:56:27 -0800 (PST) From: dmitry.semenets@gmail.com To: xen-devel@lists.xenproject.org Cc: Oleksandr Andrushchenko , Wei Liu , Anthony PERARD , Juergen Gross , Dmytro Semenets Subject: [RFC PATCH v4 1/9] tools: allow vchan XenStore paths more then 64 bytes long Date: Thu, 9 Feb 2023 17:55:55 +0200 Message-Id: <20230209155604.2753219-2-dmitry.semenets@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209155604.2753219-1-dmitry.semenets@gmail.com> References: <20230209155604.2753219-1-dmitry.semenets@gmail.com> MIME-Version: 1.0 From: Oleksandr Andrushchenko Current vchan implementation, while dealing with XenStore paths, allocates 64 bytes buffer on the stack which may not be enough for some use-cases. Make the buffer longer to respect maximum allowed XenStore path of XENSTORE_ABS_PATH_MAX. Signed-off-by: Oleksandr Andrushchenko Signed-off-by: Dmytro Semenets --- tools/libs/vchan/init.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/tools/libs/vchan/init.c b/tools/libs/vchan/init.c index 9195bd3b98..fec650dc32 100644 --- a/tools/libs/vchan/init.c +++ b/tools/libs/vchan/init.c @@ -249,7 +249,7 @@ static int init_xs_srv(struct libxenvchan *ctrl, int domain, const char* xs_base int ret = -1; struct xs_handle *xs; struct xs_permissions perms[2]; - char buf[64]; + char *buf; char ref[16]; char* domid_str = NULL; xs_transaction_t xs_trans = XBT_NULL; @@ -259,6 +259,10 @@ static int init_xs_srv(struct libxenvchan *ctrl, int domain, const char* xs_base if (!ctrl->xs_path) return -1; + buf = malloc(XENSTORE_ABS_PATH_MAX); + if (!buf) + return -1; + xs = xs_open(0); if (!xs) goto fail; @@ -280,14 +284,14 @@ retry_transaction: goto fail_xs_open; snprintf(ref, sizeof ref, "%d", ring_ref); - snprintf(buf, sizeof buf, "%s/ring-ref", xs_base); + snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/ring-ref", xs_base); if (!xs_write(xs, xs_trans, buf, ref, strlen(ref))) goto fail_xs_open; if (!xs_set_permissions(xs, xs_trans, buf, perms, 2)) goto fail_xs_open; snprintf(ref, sizeof ref, "%d", ctrl->event_port); - snprintf(buf, sizeof buf, "%s/event-channel", xs_base); + snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/event-channel", xs_base); if (!xs_write(xs, xs_trans, buf, ref, strlen(ref))) goto fail_xs_open; if (!xs_set_permissions(xs, xs_trans, buf, perms, 2)) @@ -303,6 +307,7 @@ retry_transaction: free(domid_str); xs_close(xs); fail: + free(buf); return ret; } @@ -419,13 +424,20 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger, { struct libxenvchan *ctrl = malloc(sizeof(struct libxenvchan)); struct xs_handle *xs = NULL; - char buf[64]; + char *buf; char *ref; int ring_ref; unsigned int len; if (!ctrl) - return 0; + return NULL; + + buf = malloc(XENSTORE_ABS_PATH_MAX); + if (!buf) { + free(ctrl); + return NULL; + } + ctrl->ring = NULL; ctrl->event = NULL; ctrl->gnttab = NULL; @@ -436,8 +448,9 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger, if (!xs) goto fail; + // find xenstore entry - snprintf(buf, sizeof buf, "%s/ring-ref", xs_path); + snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/ring-ref", xs_path); ref = xs_read(xs, 0, buf, &len); if (!ref) goto fail; @@ -445,7 +458,7 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger, free(ref); if (!ring_ref) goto fail; - snprintf(buf, sizeof buf, "%s/event-channel", xs_path); + snprintf(buf, XENSTORE_ABS_PATH_MAX, "%s/event-channel", xs_path); ref = xs_read(xs, 0, buf, &len); if (!ref) goto fail; @@ -475,6 +488,7 @@ struct libxenvchan *libxenvchan_client_init(struct xentoollog_logger *logger, out: if (xs) xs_close(xs); + free(buf); return ctrl; fail: libxenvchan_close(ctrl); From patchwork Thu Feb 9 15:55:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmytro Semenets X-Patchwork-Id: 13134829 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.xenproject.org (lists.xenproject.org [192.237.175.120]) (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 4E5B3C636D6 for ; Thu, 9 Feb 2023 15:56:51 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.492671.762336 (Exim 4.92) (envelope-from ) id 1pQ9Hl-0007BE-E7; Thu, 09 Feb 2023 15:56:33 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 492671.762336; Thu, 09 Feb 2023 15:56:33 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pQ9Hl-0007B7-7y; Thu, 09 Feb 2023 15:56:33 +0000 Received: by outflank-mailman (input) for mailman id 492671; Thu, 09 Feb 2023 15:56:31 +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 1pQ9Hj-0006cl-Fc for xen-devel@lists.xenproject.org; Thu, 09 Feb 2023 15:56:31 +0000 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [2a00:1450:4864:20::630]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 50ba1841-a892-11ed-93b5-47a8fe42b414; Thu, 09 Feb 2023 16:56:29 +0100 (CET) Received: by mail-ej1-x630.google.com with SMTP id hx15so7652455ejc.11 for ; Thu, 09 Feb 2023 07:56:29 -0800 (PST) Received: from dsemenets-HP-EliteBook-850-G8-Notebook-PC.. ([91.219.254.72]) by smtp.gmail.com with ESMTPSA id v1-20020a170906b00100b0087862f45a29sm1027594ejy.174.2023.02.09.07.56.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Feb 2023 07:56:27 -0800 (PST) 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 Sender: "Xen-devel" X-Inumbo-ID: 50ba1841-a892-11ed-93b5-47a8fe42b414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=03O/96QsOqq4aLYpGvEXu2Bk6X9D5zYH6rdH2Rhto3Q=; b=HH8rRKbcQejrQ139t78ATSMAMZyF0w+2salzQaCKfYP2KbwZ0i+OK4nFIL2kX38QlT 8qKK0TsA4htSkcN6otudBu2dWYjAXsVHIUlZiTwD+GnugBNKj/G9jw1a5uYoDxuHj01z Fn54o+FVreKOb6B1xAZQX2z763SYtxj0AZnM70Kx0ZlNf9nthoguVFUHhGov3mQZGPT8 NtyiD9vASz0I4uHl6gFZLJAWFArjFtABP/EY3SPR3C+j9zc/FksPC6LX4ZNY8BlyGuOx /AUHLz2CFZ9qB+eWr/36bKITxV2vzdZzm+4hyATaYaV227qbX36U2uOTsx1EMEQEr3FZ UwWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=03O/96QsOqq4aLYpGvEXu2Bk6X9D5zYH6rdH2Rhto3Q=; b=i8qUtyo12LgN90rxp1VZvnGJgJ9v9CoXahuiR4rlTsHUXqB/hRIYjR6mmmKNKpjjt/ /zgYKlUhNWT+ZO3ZwRjHqljz8ozkw9FjrwMSQPSchatPuOZHqoGgmUQGTs4JDgdDewwd 2b8nOrp5sYcdU1lzXRKk4kHdjX/eCzRgcI2XU+iZNWozN8i4U5U9CS84PG0S57OD1GeO gYZ+9vS+RgugMbVhMH1SnYApmA6c0k0KmEA77Novs4m5HxCgF20ftcQGdJrfJH7rVK2d a/pSHolJjHqZc4Z5XfmvLQ20kJVHKlshfvOkG3nigln/vIdPYBqP2L4tv5zUzigO1Lux UpIQ== X-Gm-Message-State: AO0yUKUA7YJkGMboVI7etZzS0s8w6sFE8tq0I+eJXnsgoJsUveZeuix1 yCxQN/IcbPo94XjAwctB+T2ri4iL0+7LLQ== X-Google-Smtp-Source: AK7set+LrEMplV6Q2EZwwQvoyOuh1xvJjRc43Jm6NbMsOKFIqs4HGgniioDQpgURAH79l0GC4g4gWw== X-Received: by 2002:a17:906:283:b0:878:5946:c0ac with SMTP id 3-20020a170906028300b008785946c0acmr11785944ejf.3.1675958188282; Thu, 09 Feb 2023 07:56:28 -0800 (PST) From: dmitry.semenets@gmail.com To: xen-devel@lists.xenproject.org Cc: Dmytro Semenets , Wei Liu , Anthony PERARD , Juergen Gross , Oleksandr Andrushchenko , Anastasiia Lukianenko Subject: [RFC PATCH v4 2/9] tools/libs/light: Add vchan support to libxl Date: Thu, 9 Feb 2023 17:55:56 +0200 Message-Id: <20230209155604.2753219-3-dmitry.semenets@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209155604.2753219-1-dmitry.semenets@gmail.com> References: <20230209155604.2753219-1-dmitry.semenets@gmail.com> MIME-Version: 1.0 From: Dmytro Semenets Add possibility to send commands from libxl and execute them on server side. Libxl vchan adds support for JSON messages processing. The using of libxl vchan is preventing the client from libxl from reading and writing from / to the local sysfs directly.To do this, the libxl vchan was added - instead of working with the read and write functions from / to the sysfs, functionality allows to send requests to the server, then receive and process the response. Signed-off-by: Oleksandr Andrushchenko Signed-off-by: Anastasiia Lukianenko Signed-off-by: Dmytro Semenets --- tools/helpers/Makefile | 4 +- tools/libs/light/Makefile | 7 +- tools/libs/light/libxl_vchan.c | 488 +++++++++++++++++++++++++++++++++ tools/libs/light/libxl_vchan.h | 92 +++++++ tools/xl/Makefile | 3 +- 5 files changed, 588 insertions(+), 6 deletions(-) create mode 100644 tools/libs/light/libxl_vchan.c create mode 100644 tools/libs/light/libxl_vchan.h diff --git a/tools/helpers/Makefile b/tools/helpers/Makefile index 09590eb5b6..2bf52d055d 100644 --- a/tools/helpers/Makefile +++ b/tools/helpers/Makefile @@ -20,7 +20,7 @@ $(XEN_INIT_DOM0_OBJS): CFLAGS += $(CFLAGS_libxentoollog) $(XEN_INIT_DOM0_OBJS): CFLAGS += $(CFLAGS_libxenstore) $(XEN_INIT_DOM0_OBJS): CFLAGS += $(CFLAGS_libxenlight) $(XEN_INIT_DOM0_OBJS): CFLAGS += $(CFLAGS_libxenctrl) -xen-init-dom0: LDLIBS += $(call xenlibs-ldlibs,ctrl toollog store light) +xen-init-dom0: LDLIBS += $(call xenlibs-ldlibs,ctrl toollog store light vchan) INIT_XENSTORE_DOMAIN_OBJS = init-xenstore-domain.o init-dom-json.o $(INIT_XENSTORE_DOMAIN_OBJS): CFLAGS += $(CFLAGS_libxentoollog) @@ -29,7 +29,7 @@ $(INIT_XENSTORE_DOMAIN_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(INIT_XENSTORE_DOMAIN_OBJS): CFLAGS += $(CFLAGS_libxenstore) $(INIT_XENSTORE_DOMAIN_OBJS): CFLAGS += $(CFLAGS_libxenlight) $(INIT_XENSTORE_DOMAIN_OBJS): CFLAGS += -include $(XEN_ROOT)/tools/config.h -init-xenstore-domain: LDLIBS += $(call xenlibs-ldlibs,toollog store ctrl guest light) +init-xenstore-domain: LDLIBS += $(call xenlibs-ldlibs,toollog store ctrl guest light vchan) INIT_DOM0LESS_OBJS = init-dom0less.o init-dom-json.o $(INIT_DOM0LESS_OBJS): CFLAGS += $(CFLAGS_libxentoollog) diff --git a/tools/libs/light/Makefile b/tools/libs/light/Makefile index 4fddcc6f51..0941ad2cf4 100644 --- a/tools/libs/light/Makefile +++ b/tools/libs/light/Makefile @@ -71,6 +71,7 @@ OBJS-y += libxl.o OBJS-y += libxl_create.o OBJS-y += libxl_dm.o OBJS-y += libxl_pci.o +OBJS-y += libxl_vchan.o OBJS-y += libxl_dom.o OBJS-y += libxl_exec.o OBJS-y += libxl_xshelp.o @@ -170,7 +171,7 @@ LDLIBS-y += $(PTHREAD_LIBS) LDLIBS-y += -lyajl LDLIBS += $(LDLIBS-y) -$(OBJS-y) $(PIC_OBJS) $(LIBXL_TEST_OBJS): CFLAGS += $(CFLAGS_LIBXL) -include $(XEN_ROOT)/tools/config.h +$(OBJS-y) $(PIC_OBJS) $(LIBXL_TEST_OBJS): CFLAGS += $(CFLAGS_LIBXL) $(CFLAGS_libxenevtchn) -include $(XEN_ROOT)/tools/config.h $(ACPI_OBJS) $(ACPI_PIC_OBJS): CFLAGS += -I. -DLIBACPI_STDUTILS=\"$(CURDIR)/libxl_x86_acpi.h\" $(TEST_PROG_OBJS) _libxl.api-for-check: CFLAGS += $(CFLAGS_libxentoollog) $(CFLAGS_libxentoolcore) libxl_dom.o libxl_dom.opic: CFLAGS += -I$(XEN_ROOT)/tools # include libacpi/x86.h @@ -241,13 +242,13 @@ libxenlight_test.so: $(PIC_OBJS) $(LIBXL_TEST_OBJS) $(CC) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenlight.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS) $(APPEND_LDFLAGS) test_%: test_%.o test_common.o libxenlight_test.so - $(CC) $(LDFLAGS) -o $@ $^ $(filter-out %libxenlight.so, $(LDLIBS_libxenlight)) $(LDLIBS_libxentoollog) $(LDLIBS_libxentoolcore) -lyajl $(APPEND_LDFLAGS) + $(CC) $(LDFLAGS) -o $@ $^ $(filter-out %libxenlight.so, $(LDLIBS_libxenlight)) $(LDLIBS_libxentoollog) $(LDLIBS_libxentoolcore) $(LDLIBS_libxenvchan) -lyajl $(APPEND_LDFLAGS) libxl-save-helper: $(SAVE_HELPER_OBJS) libxenlight.so $(CC) $(LDFLAGS) -o $@ $(SAVE_HELPER_OBJS) $(LDLIBS_libxentoollog) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxentoolcore) $(APPEND_LDFLAGS) testidl: testidl.o libxenlight.so - $(CC) $(LDFLAGS) -o $@ testidl.o $(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) $(LDLIBS_libxentoolcore) $(APPEND_LDFLAGS) + $(CC) $(LDFLAGS) -o $@ testidl.o $(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) $(LDLIBS_libxentoolcore) $(LDLIBS_libxenvchan) $(APPEND_LDFLAGS) install:: $(LIBHEADERS) libxl-save-helper $(INSTALL_DIR) $(DESTDIR)$(LIBEXEC_BIN) diff --git a/tools/libs/light/libxl_vchan.c b/tools/libs/light/libxl_vchan.c new file mode 100644 index 0000000000..7611816f52 --- /dev/null +++ b/tools/libs/light/libxl_vchan.c @@ -0,0 +1,488 @@ +/* + * Vchan support for JSON messages processing + * + * Copyright (C) 2021 EPAM Systems Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; version 2.1 only. with the special + * exception on linking described in file LICENSE. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + */ + +#include "libxl_osdeps.h" /* must come before any other headers */ + +#include "libxl_internal.h" +#include "libxl_vchan.h" + +#define VCHAN_EOM "\r\n" +/* + * http://xenbits.xen.org/docs/unstable/misc/xenstore-paths.html + * 1.4.4 Domain Controlled Paths + * 1.4.4.1 ~/data [w] + * A domain writable path. Available for arbitrary domain use. + */ +#define VCHAN_SRV_DIR "/local/domain" + +struct vchan_state { + struct libxenvchan *ctrl; + + /* Server domain ID. */ + libxl_domid domid; + + /* XenStore path of the server with the ring buffer and event channel. */ + char *xs_path; + + int select_fd; + + /* GC used for state's lifetime allocations, such as rx_buf. */ + libxl__gc *gc; + /* Receive buffer. */ + char *rx_buf; + /* Current allocated size. */ + size_t rx_buf_size; + /* Actual data in the buffer. */ + size_t rx_buf_used; + + /* YAJL generator used to parse and create requests/replies. */ + yajl_gen gen; +}; + +int libxl__vchan_field_add_string(libxl__gc *gc, yajl_gen gen, + const char *field, char *val) +{ + libxl__json_object *result; + + libxl__yajl_gen_asciiz(gen, field); + result = libxl__json_object_alloc(gc, JSON_STRING); + result->u.string = val; + return libxl__json_object_to_yajl_gen(gc, gen, result); +} + +static libxl__json_object *libxl__vchan_arg_new(libxl__gc *gc, + libxl__json_node_type type, + libxl__json_object *args, + char *key) +{ + libxl__json_map_node *arg; + libxl__json_object *obj; + + obj = libxl__json_object_alloc(gc, type); + + GCNEW(arg); + + arg->map_key = key; + arg->obj = obj; + + flexarray_append(args->u.map, arg); + + return obj; +} + +void libxl__vchan_arg_add_string(libxl__gc *gc, libxl__json_object *args, + char *key, char *val) +{ + libxl__json_object *obj = libxl__vchan_arg_new(gc, JSON_STRING, args, key); + + obj->u.string = val; +} + +void libxl__vchan_arg_add_bool(libxl__gc *gc, libxl__json_object *args, + char *key, bool val) +{ + libxl__json_object *obj = libxl__vchan_arg_new(gc, JSON_BOOL, args, key); + + obj->u.b = val; +} + +static void reset_yajl_generator(struct vchan_state *state) +{ + yajl_gen_clear(state->gen); + yajl_gen_reset(state->gen, NULL); +} + +void vchan_dump_gen(libxl__gc *gc, yajl_gen gen) +{ + const unsigned char *buf = NULL; + size_t len = 0; + + yajl_gen_get_buf(gen, &buf, &len); + LOG(DEBUG, "%s\n", buf); +} + +void vchan_dump_state(libxl__gc *gc, struct vchan_state *state) +{ + vchan_dump_gen(gc, state->gen); +} + +/* + * Find a JSON object and store it in o_r. + * return ERROR_NOTFOUND if no object is found. + */ +static int vchan_get_next_msg(libxl__gc *gc, struct vchan_state *state, + libxl__json_object **o_r) +{ + size_t len; + char *end = NULL; + const size_t eoml = sizeof(VCHAN_EOM) - 1; + libxl__json_object *o = NULL; + + if (!state->rx_buf_used) + return ERROR_NOTFOUND; + + /* Search for the end of a message which is CRLF. */ + end = memmem(state->rx_buf, state->rx_buf_used, VCHAN_EOM, eoml); + if (!end) + return ERROR_NOTFOUND; + + len = (end - state->rx_buf) + eoml; + + LOGD(DEBUG, state->domid, "parsing %zuB: '%.*s'", len, (int)len, + state->rx_buf); + + /* Replace \r by \0 so that libxl__json_parse can use strlen */ + state->rx_buf[len - eoml] = '\0'; + + o = libxl__json_parse(gc, state->rx_buf); + state->rx_buf_used -= len; + if (!o) { + LOGD(ERROR, state->domid, "Parse error"); + /* + * In case of parsing error get back to a known state: + * reset the buffer and continue reading. + */ + return ERROR_INVAL; + } + + memmove(state->rx_buf, state->rx_buf + len, state->rx_buf_used); + + LOGD(DEBUG, state->domid, "JSON object received: %s", JSON(o)); + + *o_r = o; + + return 0; +} + +static int vchan_process_packet(libxl__gc *gc, struct vchan_info *vchan, + libxl__json_object **resp_result) +{ + while (true) { + struct vchan_state *state = vchan->state; + int rc; + ssize_t r; + + if (!libxenvchan_is_open(state->ctrl)) + return ERROR_FAIL; + + /* Check if the buffer still has space or increase its size. */ + if (state->rx_buf_size - state->rx_buf_used < vchan->receive_buf_size) { + size_t newsize = state->rx_buf_size * 2 + vchan->receive_buf_size; + + if (newsize > vchan->max_buf_size) { + LOGD(ERROR, state->domid, + "receive buffer is too big (%zu > %zu)", + newsize, vchan->max_buf_size); + return ERROR_NOMEM; + } + + state->rx_buf_size = newsize; + state->rx_buf = libxl__realloc(state->gc, state->rx_buf, + state->rx_buf_size); + } + + do { + libxl__json_object *msg; + + r = libxenvchan_read(state->ctrl, + state->rx_buf + state->rx_buf_used, + state->rx_buf_size - state->rx_buf_used); + + if (r < 0) { + LOGED(ERROR, state->domid, "error reading"); + return ERROR_FAIL; + } else if (r == 0) + continue; + + LOG(DEBUG, "received %zdB: '%.*s'", r, + (int)r, state->rx_buf + state->rx_buf_used); + + state->rx_buf_used += r; + assert(state->rx_buf_used <= state->rx_buf_size); + + /* parse rx buffer to find one json object */ + rc = vchan_get_next_msg(gc, state, &msg); + if ((rc == ERROR_INVAL) || (rc == ERROR_NOTFOUND)) + continue; + if (rc) + return rc; + + if (resp_result) + return vchan->handle_response(gc, msg, resp_result); + else { + reset_yajl_generator(state); + return vchan->handle_request(gc, state->gen, msg); + } + } while (libxenvchan_data_ready(state->ctrl)); + } + + return 0; +} + +static int vchan_write(libxl__gc *gc, struct vchan_state *state, char *cmd) +{ + size_t len; + int ret; + + len = strlen(cmd); + while (len) { + ret = libxenvchan_write(state->ctrl, cmd, len); + if (ret < 0) { + LOGE(ERROR, "vchan write failed"); + return ERROR_FAIL; + } + cmd += ret; + len -= ret; + } + return 0; +} + +libxl__json_object *vchan_send_command(libxl__gc *gc, struct vchan_info *vchan, + char *cmd, libxl__json_object *args) +{ + libxl__json_object *result; + char *request; + int ret; + + reset_yajl_generator(vchan->state); + request = vchan->prepare_request(gc, vchan->state->gen, cmd, args); + if (!request) + return NULL; + + ret = vchan_write(gc, vchan->state, request); + if (ret < 0) + return NULL; + + ret = vchan_write(gc, vchan->state, VCHAN_EOM); + if (ret < 0) + return NULL; + + ret = vchan_process_packet(gc, vchan, &result); + if (ret < 0) + return NULL; + + return result; +} + +int vchan_process_command(libxl__gc *gc, struct vchan_info *vchan) +{ + char *json_str; + int ret; + + ret = vchan_process_packet(gc, vchan, NULL); + if (ret) + return ret; + + json_str = vchan->prepare_response(gc, vchan->state->gen); + if (!json_str) + return ERROR_INVAL; + + ret = vchan_write(gc, vchan->state, json_str); + if (ret) + return ret; + + return vchan_write(gc, vchan->state, VCHAN_EOM); +} + +static libxl_domid vchan_find_server(libxl__gc *gc, char *xs_dir, char *xs_file) +{ + char **domains; + unsigned int i, n; + libxl_domid domid = DOMID_INVALID; + + domains = libxl__xs_directory(gc, XBT_NULL, xs_dir, &n); + if (domains == NULL) + goto out; + + for (i = 0; i < n; i++) { + const char *tmp; + int d; + + if (sscanf(domains[i], "%d", &d) != 1) + continue; + + tmp = libxl__xs_read(gc, XBT_NULL, + GCSPRINTF("%s/%d/data/%s", xs_dir, d, xs_file)); + /* Found the domain where the server lives. */ + if (tmp) { + domid = d; + break; + } + } + +out: + return domid; +} + +static int vchan_init_client(libxl__gc *gc, struct vchan_state *state, + bool is_server) +{ + if (is_server) { + state->ctrl = libxenvchan_server_init(NULL, state->domid, + state->xs_path, 0, 0); + if (!state->ctrl) { + perror("Couldn't initialize vchan server"); + exit(1); + } + + } else { + state->ctrl = libxenvchan_client_init(CTX->lg, state->domid, + state->xs_path); + if (!state->ctrl) { + LOGE(ERROR, "Couldn't initialize vchan client"); + return ERROR_FAIL; + } + } + + state->ctrl->blocking = 1; + state->select_fd = libxenvchan_fd_for_select(state->ctrl); + if (state->select_fd < 0) { + LOGE(ERROR, "Couldn't read file descriptor for vchan client"); + return ERROR_FAIL; + } + + LOG(DEBUG, "Initialized vchan %s, XenSore at %s", + is_server ? "server" : "client", state->xs_path); + + return 0; +} + +struct vchan_state *vchan_init_new_state(libxl__gc *gc, libxl_domid domid, + char *vchan_xs_path, bool is_server) +{ + struct vchan_state *state; + yajl_gen gen; + int ret; + + gen = libxl_yajl_gen_alloc(NULL); + if (!gen) { + LOGE(ERROR, "Failed to allocate yajl generator"); + return NULL; + } + +#if HAVE_YAJL_V2 + /* Disable beautify for data */ + yajl_gen_config(gen, yajl_gen_beautify, 0); +#endif + + state = libxl__zalloc(gc, sizeof(*state)); + state->domid = domid; + state->xs_path = vchan_xs_path; + state->gc = gc; + ret = vchan_init_client(gc, state, is_server); + if (ret) { + state = NULL; + yajl_gen_free(gen); + } + + state->gen = gen; + + return state; +} + +char *vchan_get_server_xs_path(libxl__gc *gc, libxl_domid domid, char *srv_name) +{ + return GCSPRINTF(VCHAN_SRV_DIR "/%d/data/%s", domid, srv_name); +} + +/* + * Wait for the server to create the ring and event channel: + * since the moment we create a XS folder to the moment we start + * watching it the server may have already created the ring and + * event channel entries. Thus, we cannot watch reliably here without + * races, so poll for both entries to be created. + */ +static int vchan_wait_server_available(libxl__gc *gc, const char *xs_path) +{ + char *xs_ring, *xs_evt; + int timeout_ms = 5000; + + xs_ring = GCSPRINTF("%s/ring-ref", xs_path); + xs_evt = GCSPRINTF("%s/event-channel", xs_path); + + while (timeout_ms) { + unsigned int len; + void *file; + int entries = 0; + + file = xs_read(CTX->xsh, XBT_NULL, xs_ring, &len); + if (file) { + entries++; + free(file); + } + + file = xs_read(CTX->xsh, XBT_NULL, xs_evt, &len); + if (file) { + entries++; + free(file); + } + + if (entries == 2) + return 0; + + timeout_ms -= 10; + usleep(10000); + } + + return ERROR_TIMEDOUT; +} + +struct vchan_state *vchan_new_client(libxl__gc *gc, char *srv_name) +{ + libxl_domid domid; + char *xs_path, *vchan_xs_path; + libxl_uuid uuid; + libxl_ctx *ctx = libxl__gc_owner(gc); + + domid = vchan_find_server(gc, VCHAN_SRV_DIR, srv_name); + if (domid == DOMID_INVALID) { + LOGE(ERROR, "Can't find vchan server"); + return NULL; + } + + xs_path = vchan_get_server_xs_path(gc, domid, srv_name); + LOG(DEBUG, "vchan server at %s\n", xs_path); + + /* Generate unique client id. */ + libxl_uuid_generate(&uuid); + + vchan_xs_path = GCSPRINTF("%s/" LIBXL_UUID_FMT, xs_path, + LIBXL_UUID_BYTES((uuid))); + + if (!xs_mkdir(ctx->xsh, XBT_NULL, vchan_xs_path)) { + LOG(ERROR, "Can't create xs_dir at %s", vchan_xs_path); + return NULL; + } + + if (vchan_wait_server_available(gc, vchan_xs_path)) { + LOG(ERROR, "Failed to wait for the server to come up at %s", + vchan_xs_path); + return NULL; + } + + return vchan_init_new_state(gc, domid, vchan_xs_path, false); +} + +void vchan_fini_one(libxl__gc *gc, struct vchan_state *state) +{ + if (!state) + return; + + LOG(DEBUG, "Closing vchan"); + libxenvchan_close(state->ctrl); + + yajl_gen_free(state->gen); +} diff --git a/tools/libs/light/libxl_vchan.h b/tools/libs/light/libxl_vchan.h new file mode 100644 index 0000000000..0968875298 --- /dev/null +++ b/tools/libs/light/libxl_vchan.h @@ -0,0 +1,92 @@ +/* + Common definitions for JSON messages processing by vchan + Copyright (C) 2021 EPAM Systems Inc. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; If not, see . +*/ + +#ifndef LIBXL_VCHAN_H +#define LIBXL_VCHAN_H + +#include + +struct vchan_state; + +struct vchan_info { + struct vchan_state *state; + + /* Process request and produce the result by adding json-objects to gen .*/ + int (*handle_request)(libxl__gc *gc, yajl_gen gen, + const libxl__json_object *request); + /* Convert the prepared response into JSON string. */ + char *(*prepare_response)(libxl__gc *gc, yajl_gen gen); + + /* Prepare request as JSON string which will be sent. */ + char *(*prepare_request)(libxl__gc *gc, yajl_gen gen, char *request, + libxl__json_object *args); + /* Handle response and produce the output suitable for the requester. */ + int (*handle_response)(libxl__gc *gc, const libxl__json_object *response, + libxl__json_object **result); + + /* Handle new client connection on the server side. */ + int (*handle_new_client)(libxl__gc *gc); + + /* Buffer info. */ + size_t receive_buf_size; + size_t max_buf_size; +}; + +int libxl__vchan_field_add_string(libxl__gc *gc, yajl_gen hand, + const char *field, char *val); + +static inline libxl__json_object *libxl__vchan_start_args(libxl__gc *gc) +{ + return libxl__json_object_alloc(gc, JSON_MAP); +} + +void libxl__vchan_arg_add_string(libxl__gc *gc, libxl__json_object *args, + char *key, char *val); +void libxl__vchan_arg_add_bool(libxl__gc *gc, libxl__json_object *args, + char *key, bool val); + +libxl__json_object *vchan_send_command(libxl__gc *gc, struct vchan_info *vchan, + char *cmd, libxl__json_object *args); + +void vchan_reset_generator(struct vchan_state *state); + +int vchan_process_command(libxl__gc *gc, struct vchan_info *vchan); + +char *vchan_get_server_xs_path(libxl__gc *gc, libxl_domid domid, char *srv_name); + +struct vchan_state *vchan_init_new_state(libxl__gc *gc, libxl_domid domid, + char *vchan_xs_path, bool is_server); + +struct vchan_state *vchan_new_client(libxl__gc *gc, char *srv_name); + +void vchan_fini_one(libxl__gc *gc, struct vchan_state *state); + +void vchan_dump_state(libxl__gc *gc, struct vchan_state *state); +void vchan_dump_gen(libxl__gc *gc, yajl_gen gen); + +#endif /* LIBXL_VCHAN_H */ + +/* + * Local variables: + * mode: C + * c-file-style: "linux" + * indent-tabs-mode: t + * c-basic-offset: 8 + * tab-width: 8 + * End: + */ diff --git a/tools/xl/Makefile b/tools/xl/Makefile index 5f7aa5f46c..da4591b6a9 100644 --- a/tools/xl/Makefile +++ b/tools/xl/Makefile @@ -15,6 +15,7 @@ LDFLAGS += $(PTHREAD_LDFLAGS) CFLAGS_XL += $(CFLAGS_libxenlight) CFLAGS_XL += $(CFLAGS_libxenutil) CFLAGS_XL += -Wshadow +CFLAGS_XL += $(CFLAGS_libxenvchan) XL_OBJS-$(CONFIG_X86) = xl_psr.o XL_OBJS = xl.o xl_cmdtable.o xl_sxp.o xl_utils.o $(XL_OBJS-y) @@ -33,7 +34,7 @@ $(XL_OBJS): CFLAGS += -include $(XEN_ROOT)/tools/config.h # libxl_json.h needs i all: xl xl: $(XL_OBJS) - $(CC) $(LDFLAGS) -o $@ $(XL_OBJS) $(LDLIBS_libxenutil) $(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) -lyajl $(APPEND_LDFLAGS) + $(CC) $(LDFLAGS) -o $@ $(XL_OBJS) $(LDLIBS_libxenutil) $(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) $(LDLIBS_libxenvchan) -lyajl $(APPEND_LDFLAGS) .PHONY: install install: all From patchwork Thu Feb 9 15:55:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmytro Semenets X-Patchwork-Id: 13134833 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.xenproject.org (lists.xenproject.org [192.237.175.120]) (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 422F8C61DA4 for ; Thu, 9 Feb 2023 15:56:53 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.492672.762347 (Exim 4.92) (envelope-from ) id 1pQ9Hm-0007ST-TG; Thu, 09 Feb 2023 15:56:34 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 492672.762347; Thu, 09 Feb 2023 15:56:34 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pQ9Hm-0007SJ-O8; Thu, 09 Feb 2023 15:56:34 +0000 Received: by outflank-mailman (input) for mailman id 492672; Thu, 09 Feb 2023 15:56:33 +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 1pQ9Hl-0007Ak-2K for xen-devel@lists.xenproject.org; Thu, 09 Feb 2023 15:56:33 +0000 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [2a00:1450:4864:20::62f]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 519d4f8f-a892-11ed-933c-83870f6b2ba8; Thu, 09 Feb 2023 16:56:30 +0100 (CET) Received: by mail-ej1-x62f.google.com with SMTP id c26so2920514ejz.10 for ; Thu, 09 Feb 2023 07:56:30 -0800 (PST) Received: from dsemenets-HP-EliteBook-850-G8-Notebook-PC.. ([91.219.254.72]) by smtp.gmail.com with ESMTPSA id v1-20020a170906b00100b0087862f45a29sm1027594ejy.174.2023.02.09.07.56.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Feb 2023 07:56:29 -0800 (PST) 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 Sender: "Xen-devel" X-Inumbo-ID: 519d4f8f-a892-11ed-933c-83870f6b2ba8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=yJepENBooB8tnoAsGQEI6uVGIC9PbSV8wMNtT1M+PBA=; b=lVKyPCvi1P3BzjR0R1P0UuDTd3etXpBDEHEpBz7XajqUzfGRqEpK4po3AHGdPahnBc feUq7TBn90f16l37u1nPgV/yLIRGA5NX9pMqkhxg4XZj4tB6TAiqcqqymDcIF3Qvk6RB ILPb/cM3Ng3Q1YtDovAUKtz1OkZqVPikWXRT6m7SlWQdkcNA9Vb13DYryFoedH8FBcJD hFKZM8WmV3uE01vui46Vv3UN3kRpwrPEXG8OOVgXvtqdWRLeU3/DhcrqVfYZk3S2E1+V vdjEynY77IIIQVKifqGBl206Bb3TgRsKSb2LTpUXzjG/2Qu3bh7muNrFOvDtB8doU6Ed iVnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yJepENBooB8tnoAsGQEI6uVGIC9PbSV8wMNtT1M+PBA=; b=WMCrpxnE0t6PgSO/j5Vff/FQubXWxTvsjZDuBTiVcvJ9FJRzMz2k9F/yjKTrPXENSO tamHQUEnWo0Zl9cuWyHi/nZ/6yqSuDVs4Z5esLcUxdNB2R5pW1BwcvMgaeuHlL0hlk7g 6mqazOTzpF6RKjj+Qt7IVX9DVWsVxNHdnsOgDZlSpfpXO1xBn95OgrmdqGuggG+lbUBq zu70guwuIlkcmfz000OA+4/kHdiSPxJG2DAdF85SeTRtsb+vTIOPiobOK62i2TPO/JNd gv63Kj9pvMr9/OgfNzXrJ4/WY0+uBLYJPThoLpRo/qALHb4ExsV2exxRFlaUNPK+xEaS hsaw== X-Gm-Message-State: AO0yUKXLB2/EaB0PDo8/Vs9mztdFxq5neCWCwxk8vgxNTlSA49i5NQR4 f+Rdcor44cYtPp7YZ0moyiBOlgKIBOIn4A== X-Google-Smtp-Source: AK7set81phE31Bybl/PFIIPg1DndlV9xZvdjfkV6IeUrl6A1qoPYapdjwju8W1yUblqTS9BP/VV9og== X-Received: by 2002:a17:906:1d2:b0:893:526b:c77b with SMTP id 18-20020a17090601d200b00893526bc77bmr12880215ejj.7.1675958189713; Thu, 09 Feb 2023 07:56:29 -0800 (PST) From: dmitry.semenets@gmail.com To: xen-devel@lists.xenproject.org Cc: Dmytro Semenets , Wei Liu , Anthony PERARD , Juergen Gross , Oleksandr Andrushchenko , Anastasiia Lukianenko Subject: [RFC PATCH v4 3/9] tools/xlpcid: Add xlpcid daemon Date: Thu, 9 Feb 2023 17:55:57 +0200 Message-Id: <20230209155604.2753219-4-dmitry.semenets@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209155604.2753219-1-dmitry.semenets@gmail.com> References: <20230209155604.2753219-1-dmitry.semenets@gmail.com> MIME-Version: 1.0 From: Dmytro Semenets Add draft version of pcid server (based on vchan-node2), which can receive messages from the client. Add essential functionality to handle pcid protocol: - define required constants - prepare for handling remote requests - prepare and send an error packet Signed-off-by: Dmytro Semenets Signed-off-by: Oleksandr Andrushchenko Signed-off-by: Anastasiia Lukianenko --- tools/Makefile | 1 + tools/configure | 8 +- tools/configure.ac | 1 + tools/hotplug/FreeBSD/rc.d/xlpcid.in | 75 +++ tools/hotplug/Linux/init.d/xlpcid.in | 76 ++++ tools/hotplug/Linux/systemd/Makefile | 1 + .../hotplug/Linux/systemd/xenpcid.service.in | 10 + tools/hotplug/NetBSD/rc.d/xlpcid.in | 75 +++ tools/include/xen-pcid.h | 95 ++++ tools/libs/light/Makefile | 1 + tools/libs/light/libxl_pci.c | 128 ++++++ tools/libs/light/libxl_pcid.c | 428 ++++++++++++++++++ tools/xlpcid/Makefile | 52 +++ tools/xlpcid/bash-completion | 20 + tools/xlpcid/xlpcid.c | 213 +++++++++ 15 files changed, 1182 insertions(+), 2 deletions(-) create mode 100644 tools/hotplug/FreeBSD/rc.d/xlpcid.in create mode 100644 tools/hotplug/Linux/init.d/xlpcid.in create mode 100644 tools/hotplug/Linux/systemd/xenpcid.service.in create mode 100644 tools/hotplug/NetBSD/rc.d/xlpcid.in create mode 100644 tools/include/xen-pcid.h create mode 100644 tools/libs/light/libxl_pcid.c create mode 100644 tools/xlpcid/Makefile create mode 100644 tools/xlpcid/bash-completion create mode 100644 tools/xlpcid/xlpcid.c diff --git a/tools/Makefile b/tools/Makefile index 4906fdbc23..5a99f7bb61 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -30,6 +30,7 @@ endif SUBDIRS-y += xenpmd SUBDIRS-$(CONFIG_GOLANG) += golang SUBDIRS-y += xl +SUBDIRS-y += xlpcid SUBDIRS-y += helpers SUBDIRS-$(CONFIG_X86) += xenpaging SUBDIRS-$(CONFIG_X86) += debugger diff --git a/tools/configure b/tools/configure index dae377c982..0cd6edb6ca 100755 --- a/tools/configure +++ b/tools/configure @@ -2455,7 +2455,7 @@ ac_compiler_gnu=$ac_cv_c_compiler_gnu -ac_config_files="$ac_config_files ../config/Tools.mk hotplug/common/hotplugpath.sh hotplug/FreeBSD/rc.d/xencommons hotplug/FreeBSD/rc.d/xendriverdomain hotplug/Linux/init.d/sysconfig.xencommons hotplug/Linux/init.d/sysconfig.xendomains hotplug/Linux/init.d/xen-watchdog hotplug/Linux/init.d/xencommons hotplug/Linux/init.d/xendomains hotplug/Linux/init.d/xendriverdomain hotplug/Linux/launch-xenstore hotplug/Linux/vif-setup hotplug/Linux/xen-hotplug-common.sh hotplug/Linux/xendomains hotplug/NetBSD/rc.d/xencommons hotplug/NetBSD/rc.d/xendriverdomain ocaml/libs/xs/paths.ml ocaml/xenstored/paths.ml ocaml/xenstored/oxenstored.conf" +ac_config_files="$ac_config_files ../config/Tools.mk hotplug/common/hotplugpath.sh hotplug/FreeBSD/rc.d/xencommons hotplug/FreeBSD/rc.d/xendriverdomain hotplug/FreeBSD/rc.d/xlpcid hotplug/Linux/init.d/sysconfig.xencommons hotplug/Linux/init.d/sysconfig.xendomains hotplug/Linux/init.d/xlpcid hotplug/Linux/init.d/xen-watchdog hotplug/Linux/init.d/xencommons hotplug/Linux/init.d/xendomains hotplug/Linux/init.d/xendriverdomain hotplug/Linux/launch-xenstore hotplug/Linux/vif-setup hotplug/Linux/xen-hotplug-common.sh hotplug/Linux/xendomains hotplug/NetBSD/rc.d/xencommons hotplug/NetBSD/rc.d/xendriverdomain hotplug/NetBSD/rc.d/xlpcid ocaml/libs/xs/paths.ml ocaml/xenstored/paths.ml ocaml/xenstored/oxenstored.conf" ac_config_headers="$ac_config_headers config.h" @@ -10081,7 +10081,7 @@ fi if test "x$systemd" = "xy"; then : - ac_config_files="$ac_config_files hotplug/Linux/systemd/proc-xen.mount hotplug/Linux/systemd/xen-init-dom0.service hotplug/Linux/systemd/xen-qemu-dom0-disk-backend.service hotplug/Linux/systemd/xen-watchdog.service hotplug/Linux/systemd/xenconsoled.service hotplug/Linux/systemd/xendomains.service hotplug/Linux/systemd/xendriverdomain.service hotplug/Linux/systemd/xenstored.service" + ac_config_files="$ac_config_files hotplug/Linux/systemd/proc-xen.mount hotplug/Linux/systemd/xen-init-dom0.service hotplug/Linux/systemd/xen-qemu-dom0-disk-backend.service hotplug/Linux/systemd/xen-watchdog.service hotplug/Linux/systemd/xenconsoled.service hotplug/Linux/systemd/xendomains.service hotplug/Linux/systemd/xendriverdomain.service hotplug/Linux/systemd/xenstored.service hotplug/Linux/systemd/xenpcid.service" fi @@ -10946,8 +10946,10 @@ do "hotplug/common/hotplugpath.sh") CONFIG_FILES="$CONFIG_FILES hotplug/common/hotplugpath.sh" ;; "hotplug/FreeBSD/rc.d/xencommons") CONFIG_FILES="$CONFIG_FILES hotplug/FreeBSD/rc.d/xencommons" ;; "hotplug/FreeBSD/rc.d/xendriverdomain") CONFIG_FILES="$CONFIG_FILES hotplug/FreeBSD/rc.d/xendriverdomain" ;; + "hotplug/FreeBSD/rc.d/xlpcid") CONFIG_FILES="$CONFIG_FILES hotplug/FreeBSD/rc.d/xlpcid" ;; "hotplug/Linux/init.d/sysconfig.xencommons") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/init.d/sysconfig.xencommons" ;; "hotplug/Linux/init.d/sysconfig.xendomains") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/init.d/sysconfig.xendomains" ;; + "hotplug/Linux/init.d/xlpcid") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/init.d/xlpcid" ;; "hotplug/Linux/init.d/xen-watchdog") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/init.d/xen-watchdog" ;; "hotplug/Linux/init.d/xencommons") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/init.d/xencommons" ;; "hotplug/Linux/init.d/xendomains") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/init.d/xendomains" ;; @@ -10958,6 +10960,7 @@ do "hotplug/Linux/xendomains") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/xendomains" ;; "hotplug/NetBSD/rc.d/xencommons") CONFIG_FILES="$CONFIG_FILES hotplug/NetBSD/rc.d/xencommons" ;; "hotplug/NetBSD/rc.d/xendriverdomain") CONFIG_FILES="$CONFIG_FILES hotplug/NetBSD/rc.d/xendriverdomain" ;; + "hotplug/NetBSD/rc.d/xlpcid") CONFIG_FILES="$CONFIG_FILES hotplug/NetBSD/rc.d/xlpcid" ;; "ocaml/libs/xs/paths.ml") CONFIG_FILES="$CONFIG_FILES ocaml/libs/xs/paths.ml" ;; "ocaml/xenstored/paths.ml") CONFIG_FILES="$CONFIG_FILES ocaml/xenstored/paths.ml" ;; "ocaml/xenstored/oxenstored.conf") CONFIG_FILES="$CONFIG_FILES ocaml/xenstored/oxenstored.conf" ;; @@ -10970,6 +10973,7 @@ do "hotplug/Linux/systemd/xendomains.service") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/xendomains.service" ;; "hotplug/Linux/systemd/xendriverdomain.service") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/xendriverdomain.service" ;; "hotplug/Linux/systemd/xenstored.service") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/xenstored.service" ;; + "hotplug/Linux/systemd/xenpcid.service") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/xenpcid.service" ;; *) as_fn_error $? "invalid argument: \`$ac_config_target'" "$LINENO" 5;; esac diff --git a/tools/configure.ac b/tools/configure.ac index 3a2f6a2da9..d2b22e94a9 100644 --- a/tools/configure.ac +++ b/tools/configure.ac @@ -489,6 +489,7 @@ AS_IF([test "x$systemd" = "xy"], [ hotplug/Linux/systemd/xendomains.service hotplug/Linux/systemd/xendriverdomain.service hotplug/Linux/systemd/xenstored.service + hotplug/Linux/systemd/xenpcid.service ]) ]) diff --git a/tools/hotplug/FreeBSD/rc.d/xlpcid.in b/tools/hotplug/FreeBSD/rc.d/xlpcid.in new file mode 100644 index 0000000000..ad414c25ba --- /dev/null +++ b/tools/hotplug/FreeBSD/rc.d/xlpcid.in @@ -0,0 +1,75 @@ +#! /bin/bash +# +# xlpcid +# +# description: Run xlpcid daemon +### BEGIN INIT INFO +# Provides: xlpcid +# Short-Description: Start/stop xlpcid +# Description: Run xlpcid daemon +### END INIT INFO +# + +. @XEN_SCRIPT_DIR@/hotplugpath.sh + +xencommons_config=@CONFIG_DIR@/@CONFIG_LEAF_DIR@ + +test -f $xencommons_config/xencommons && . $xencommons_config/xencommons + +XLPCID_PIDFILE="@XEN_RUN_DIR@/xlpcid.pid" + +# Source function library. +if [ -e /etc/init.d/functions ] ; then + . /etc/init.d/functions +elif [ -e /lib/lsb/init-functions ] ; then + . /lib/lsb/init-functions + success () { + log_success_msg $* + } + failure () { + log_failure_msg $* + } +else + success () { + echo $* + } + failure () { + echo $* + } +fi + +start() { + echo Starting xlpcid... + ${sbindir}/xlpcid --pidfile=$XLPCID_PIDFILE $XLPCID_ARGS +} + +stop() { + echo Stopping xl pcid... + if read 2>/dev/null <$XLPCID_PIDFILE pid; then + kill $pid + while kill -9 $pid >/dev/null 2>&1; do sleep 1; done + rm -f $XLPCID_PIDFILE + fi +} + +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart) + stop + start + ;; + status) + ;; + condrestart) + stop + start + ;; + *) + echo $"Usage: $0 {start|stop|status|restart|condrestart}" + exit 1 +esac diff --git a/tools/hotplug/Linux/init.d/xlpcid.in b/tools/hotplug/Linux/init.d/xlpcid.in new file mode 100644 index 0000000000..d8e38501a7 --- /dev/null +++ b/tools/hotplug/Linux/init.d/xlpcid.in @@ -0,0 +1,76 @@ +#! /bin/bash +# +# xlpcid +# +# description: Run xlpcid daemon +### BEGIN INIT INFO +# Provides: xlpcid +# Short-Description: Start/stop xlpcid +# Description: Run xlpcid daemon +### END INIT INFO +# + +. @XEN_SCRIPT_DIR@/hotplugpath.sh + +xencommons_config=@CONFIG_DIR@/@CONFIG_LEAF_DIR@ + +test -f $xencommons_config/xencommons && . $xencommons_config/xencommons + +XLPCID_PIDFILE="@XEN_RUN_DIR@/xlpcid.pid" + +# Source function library. +if [ -e /etc/init.d/functions ] ; then + . /etc/init.d/functions +elif [ -e /lib/lsb/init-functions ] ; then + . /lib/lsb/init-functions + success () { + log_success_msg $* + } + failure () { + log_failure_msg $* + } +else + success () { + echo $* + } + failure () { + echo $* + } +fi + +start() { + echo Starting xlpcid... + ${sbindir}/xlpcid --pidfile=$XLPCID_PIDFILE $XLPCID_ARGS +} + +stop() { + echo Stopping xl pcid... + if read 2>/dev/null <$XLPCID_PIDFILE pid; then + kill $pid + while kill -9 $pid >/dev/null 2>&1; do sleep 1; done + rm -f $XLPCID_PIDFILE + fi +} + +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart) + stop + start + ;; + status) + ;; + condrestart) + stop + start + ;; + *) + echo $"Usage: $0 {start|stop|status|restart|condrestart}" + exit 1 +esac + diff --git a/tools/hotplug/Linux/systemd/Makefile b/tools/hotplug/Linux/systemd/Makefile index e29889156d..49f0f87296 100644 --- a/tools/hotplug/Linux/systemd/Makefile +++ b/tools/hotplug/Linux/systemd/Makefile @@ -12,6 +12,7 @@ XEN_SYSTEMD_SERVICE += xendomains.service XEN_SYSTEMD_SERVICE += xen-watchdog.service XEN_SYSTEMD_SERVICE += xen-init-dom0.service XEN_SYSTEMD_SERVICE += xendriverdomain.service +XEN_SYSTEMD_SERVICE += xenpcid.service ALL_XEN_SYSTEMD := $(XEN_SYSTEMD_MODULES) \ $(XEN_SYSTEMD_MOUNT) \ diff --git a/tools/hotplug/Linux/systemd/xenpcid.service.in b/tools/hotplug/Linux/systemd/xenpcid.service.in new file mode 100644 index 0000000000..b0bfd72734 --- /dev/null +++ b/tools/hotplug/Linux/systemd/xenpcid.service.in @@ -0,0 +1,10 @@ +[Unit] +Description=Xen PCI host daemon +ConditionVirtualization=xen + +[Service] +Type=forking +ExecStart=@sbindir@/xlpcid + +[Install] +WantedBy=multi-user.target diff --git a/tools/hotplug/NetBSD/rc.d/xlpcid.in b/tools/hotplug/NetBSD/rc.d/xlpcid.in new file mode 100644 index 0000000000..ad414c25ba --- /dev/null +++ b/tools/hotplug/NetBSD/rc.d/xlpcid.in @@ -0,0 +1,75 @@ +#! /bin/bash +# +# xlpcid +# +# description: Run xlpcid daemon +### BEGIN INIT INFO +# Provides: xlpcid +# Short-Description: Start/stop xlpcid +# Description: Run xlpcid daemon +### END INIT INFO +# + +. @XEN_SCRIPT_DIR@/hotplugpath.sh + +xencommons_config=@CONFIG_DIR@/@CONFIG_LEAF_DIR@ + +test -f $xencommons_config/xencommons && . $xencommons_config/xencommons + +XLPCID_PIDFILE="@XEN_RUN_DIR@/xlpcid.pid" + +# Source function library. +if [ -e /etc/init.d/functions ] ; then + . /etc/init.d/functions +elif [ -e /lib/lsb/init-functions ] ; then + . /lib/lsb/init-functions + success () { + log_success_msg $* + } + failure () { + log_failure_msg $* + } +else + success () { + echo $* + } + failure () { + echo $* + } +fi + +start() { + echo Starting xlpcid... + ${sbindir}/xlpcid --pidfile=$XLPCID_PIDFILE $XLPCID_ARGS +} + +stop() { + echo Stopping xl pcid... + if read 2>/dev/null <$XLPCID_PIDFILE pid; then + kill $pid + while kill -9 $pid >/dev/null 2>&1; do sleep 1; done + rm -f $XLPCID_PIDFILE + fi +} + +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart) + stop + start + ;; + status) + ;; + condrestart) + stop + start + ;; + *) + echo $"Usage: $0 {start|stop|status|restart|condrestart}" + exit 1 +esac diff --git a/tools/include/xen-pcid.h b/tools/include/xen-pcid.h new file mode 100644 index 0000000000..098118a0c3 --- /dev/null +++ b/tools/include/xen-pcid.h @@ -0,0 +1,95 @@ +/* + Common definitions for Xen PCI client-server protocol. + Copyright (C) 2021 EPAM Systems Inc. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + SPDX-License-Identifier: LGPL-2.1+ +*/ + +#ifndef XEN_PCID_H +#define XEN_PCID_H + +#define PCID_SRV_NAME "pcid" +#define PCID_XS_TOKEN "pcid-token" + +#define PCI_RECEIVE_BUFFER_SIZE 4096 +#define PCI_MAX_SIZE_RX_BUF MB(1) + +/* + ******************************************************************************* + * Common request and response structures used be the pcid remote protocol are + * described below. + ******************************************************************************* + * Request: + * +-------------+--------------+----------------------------------------------+ + * | Field | Type | Comment | + * +-------------+--------------+----------------------------------------------+ + * | cmd | string | String identifying the command | + * +-------------+--------------+----------------------------------------------+ + * + * Response: + * +-------------+--------------+----------------------------------------------+ + * | Field | Type | Comment | + * +-------------+--------------+----------------------------------------------+ + * | resp | string | Command string as in the request | + * +-------------+--------------+----------------------------------------------+ + * | error | string | "okay", "failed" | + * +-------------+--------------+----------------------------------------------+ + * | error_desc | string | Optional error description string | + * +-------------+--------------+----------------------------------------------+ + * + * Notes. + * 1. Every request and response must contain the above mandatory structures. + * 2. In case if a bad packet or an unknown command received by the server side + * a valid reply with the corresponding error code must be sent to the client. + * + * Requests and responses, which require SBDF as part of their payload, must + * use the following convention for encoding SBDF value: + * + * pci_device object: + * +-------------+--------------+----------------------------------------------+ + * | Field | Type | Comment | + * +-------------+--------------+----------------------------------------------+ + * | sbdf | string | SBDF string in form SSSS:BB:DD.F | + * +-------------+--------------+----------------------------------------------+ + */ + +/* Fileld names */ +#define PCID_MSG_FIELD_CMD "cmd" + +#define PCID_MSG_FIELD_RESP "resp" +#define PCID_MSG_FIELD_ERR "error" +#define PCID_MSG_FIELD_ERR_DESC "error_desc" + +/* pci_device object fields. */ +#define PCID_MSG_FIELD_SBDF "sbdf" + +/* Error values */ +#define PCID_MSG_ERR_OK "okay" +#define PCID_MSG_ERR_FAILED "failed" +#define PCID_MSG_ERR_NA "NA" + +#define PCID_SBDF_FMT "%04x:%02x:%02x.%01x" + +int libxl_pcid_process(libxl_ctx *ctx); + +#endif /* XEN_PCID_H */ + +/* + * Local variables: + * mode: C + * c-file-style: "linux" + * indent-tabs-mode: t + * c-basic-offset: 8 + * tab-width: 8 + * End: + */ diff --git a/tools/libs/light/Makefile b/tools/libs/light/Makefile index 0941ad2cf4..72997eaac9 100644 --- a/tools/libs/light/Makefile +++ b/tools/libs/light/Makefile @@ -71,6 +71,7 @@ OBJS-y += libxl.o OBJS-y += libxl_create.o OBJS-y += libxl_dm.o OBJS-y += libxl_pci.o +OBJS-y += libxl_pcid.o OBJS-y += libxl_vchan.o OBJS-y += libxl_dom.o OBJS-y += libxl_exec.o diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c index f4c4f17545..3d9ed0d50c 100644 --- a/tools/libs/light/libxl_pci.c +++ b/tools/libs/light/libxl_pci.c @@ -18,6 +18,10 @@ #include "libxl_internal.h" +#include + +#include "libxl_vchan.h" + #define PCI_BDF "%04x:%02x:%02x.%01x" #define PCI_BDF_SHORT "%02x:%02x.%01x" #define PCI_BDF_VDEVFN "%04x:%02x:%02x.%01x@%02x" @@ -25,6 +29,130 @@ #define PCI_BDF_XSPATH "%04x-%02x-%02x-%01x" #define PCI_PT_QDEV_ID "pci-pt-%02x_%02x.%01x" +static int pci_handle_response(libxl__gc *gc, + const libxl__json_object *response, + libxl__json_object **result) +{ + const libxl__json_object *command_obj; + const libxl__json_object *err_obj; + char *command_name; + int ret = 0; + + *result = NULL; + + command_obj = libxl__json_map_get(PCID_MSG_FIELD_RESP, response, JSON_STRING); + if (!command_obj) { + /* This is an unsupported or bad response. */ + return 0; + } + + err_obj = libxl__json_map_get(PCID_MSG_FIELD_ERR, response, JSON_STRING); + if (!err_obj) { + /* Bad packet without error code field. */ + return 0; + } + + if (strcmp(err_obj->u.string, PCID_MSG_ERR_OK) != 0) { + const libxl__json_object *err_desc_obj; + + /* The response may contain an optional error string. */ + err_desc_obj = libxl__json_map_get(PCID_MSG_FIELD_ERR_DESC, + response, JSON_STRING); + if (err_desc_obj) + LOG(ERROR, "%s", err_desc_obj->u.string); + else + LOG(ERROR, "%s", err_obj->u.string); + return ERROR_FAIL; + } + + command_name = command_obj->u.string; + LOG(DEBUG, "command: %s", command_name); + + return ret; +} + +#define CONVERT_YAJL_GEN_TO_STATUS(gen) \ + ((gen) == yajl_gen_status_ok ? yajl_status_ok : yajl_status_error) + +static char *pci_prepare_request(libxl__gc *gc, yajl_gen gen, char *cmd, + libxl__json_object *args) +{ + const unsigned char *buf; + libxl_yajl_length len; + yajl_gen_status sts; + yajl_status ret; + char *request = NULL; + int rc; + + ret = CONVERT_YAJL_GEN_TO_STATUS(yajl_gen_map_open(gen)); + if (ret != yajl_status_ok) + return NULL; + + rc = libxl__vchan_field_add_string(gc, gen, PCID_MSG_FIELD_CMD, cmd); + if (rc) + return NULL; + + if (args) { + int idx = 0; + libxl__json_map_node *node = NULL; + + assert(args->type == JSON_MAP); + for (idx = 0; idx < args->u.map->count; idx++) { + if (flexarray_get(args->u.map, idx, (void**)&node) != 0) + break; + + ret = CONVERT_YAJL_GEN_TO_STATUS(libxl__yajl_gen_asciiz(gen, node->map_key)); + if (ret != yajl_status_ok) + return NULL; + ret = libxl__json_object_to_yajl_gen(gc, gen, node->obj); + if (ret != yajl_status_ok) + return NULL; + } + } + ret = CONVERT_YAJL_GEN_TO_STATUS(yajl_gen_map_close(gen)); + if (ret != yajl_status_ok) + return NULL; + + sts = yajl_gen_get_buf(gen, &buf, &len); + if (sts != yajl_gen_status_ok) + return NULL; + + request = libxl__sprintf(gc, "%s", buf); + + vchan_dump_gen(gc, gen); + + return request; +} + +struct vchan_info *pci_vchan_get_client(libxl__gc *gc); +struct vchan_info *pci_vchan_get_client(libxl__gc *gc) +{ + struct vchan_info *vchan; + + vchan = libxl__zalloc(gc, sizeof(*vchan)); + if (!vchan) + goto out; + vchan->state = vchan_new_client(gc, PCID_SRV_NAME); + if (!(vchan->state)) { + vchan = NULL; + goto out; + } + + vchan->handle_response = pci_handle_response; + vchan->prepare_request = pci_prepare_request; + vchan->receive_buf_size = PCI_RECEIVE_BUFFER_SIZE; + vchan->max_buf_size = PCI_MAX_SIZE_RX_BUF; + +out: + return vchan; +} + +void pci_vchan_free(libxl__gc *gc, struct vchan_info *vchan); +void pci_vchan_free(libxl__gc *gc, struct vchan_info *vchan) +{ + vchan_fini_one(gc, vchan->state); +} + static unsigned int pci_encode_bdf(libxl_device_pci *pci) { unsigned int value; diff --git a/tools/libs/light/libxl_pcid.c b/tools/libs/light/libxl_pcid.c new file mode 100644 index 0000000000..2ce8f1504d --- /dev/null +++ b/tools/libs/light/libxl_pcid.c @@ -0,0 +1,428 @@ +/* + Utils for xl pcid daemon + + Copyright (C) 2021 EPAM Systems Inc. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; If not, see . + */ + +#define _GNU_SOURCE // required for strchrnul() + +#include "libxl_osdeps.h" /* must come before any other headers */ + +#include "libxl_internal.h" +#include "libxl_vchan.h" + +#include +#include + +#include + +#include +#include +#include + +#include +#include + +#define DOM0_ID 0 + +struct vchan_client { + XEN_LIST_ENTRY(struct vchan_client) list; + + /* This is the watch entry fired for this client. */ + char **watch_ret; + /* Length of the watch_ret[XS_WATCH_PATH]. */ + size_t watch_len; + + struct vchan_info info; + + /* + * This context is used by the processing loop to create its own gc + * and use it while processing commands, so we do not get OOM. + */ + libxl_ctx *ctx; + /* This gc holds all allocations made for the client needs itself. */ + libxl__gc gc[1]; + pthread_t run_thread; +}; + +static XEN_LIST_HEAD(clients_list, struct vchan_client) vchan_clients; + +static pthread_mutex_t vchan_client_mutex; + +static int make_error_reply(libxl__gc *gc, yajl_gen gen, char *desc, + char *command_name) +{ + int rc; + + rc = libxl__vchan_field_add_string(gc, gen, PCID_MSG_FIELD_RESP, + command_name); + if (rc) + return rc; + + rc = libxl__vchan_field_add_string(gc, gen, PCID_MSG_FIELD_ERR, + PCID_MSG_ERR_FAILED); + if (rc) + return rc; + + rc = libxl__vchan_field_add_string(gc, gen, PCID_MSG_FIELD_ERR_DESC, desc); + if (rc) + return rc; + + return 0; +} + +static int pcid_handle_request(libxl__gc *gc, yajl_gen gen, + const libxl__json_object *request) +{ + const libxl__json_object *command_obj; + libxl__json_object *command_response = NULL; + char *command_name; + int ret = 0; + + yajl_gen_map_open(gen); + + command_obj = libxl__json_map_get(PCID_MSG_FIELD_CMD, request, JSON_STRING); + if (!command_obj) { + /* This is an unsupported or bad request. */ + ret = make_error_reply(gc, gen, "Unsupported request or bad packet", + PCID_MSG_ERR_NA); + goto out; + } + + command_name = command_obj->u.string; + + /* + * This is an unsupported command: make a reply and proceed over + * the error path. + */ + ret = make_error_reply(gc, gen, "Unsupported command", + command_name); + if (!ret) + ret = ERROR_NOTFOUND; + + if (ret) { + /* + * The command handler on error must provide a valid response, + * so we don't need to add any other field below. + */ + ret = 0; + goto out; + } + + if (command_response) { + ret = libxl__json_object_to_yajl_gen(gc, gen, command_response); + if (ret) + goto out; + } + + ret = libxl__vchan_field_add_string(gc, gen, PCID_MSG_FIELD_RESP, + command_name); + if (ret) + goto out; + + ret = libxl__vchan_field_add_string(gc, gen, PCID_MSG_FIELD_ERR, + PCID_MSG_ERR_OK); +out: + yajl_gen_map_close(gen); + + vchan_dump_gen(gc, gen); + + return ret; +} + +static char *pcid_prepare_response(libxl__gc *gc, yajl_gen gen) +{ + const unsigned char *buf; + libxl_yajl_length len; + yajl_gen_status sts; + char *reply = NULL; + + sts = yajl_gen_get_buf(gen, &buf, &len); + if (sts != yajl_gen_status_ok) + goto out; + + reply = libxl__sprintf(gc, "%s", buf); + + vchan_dump_gen(gc, gen); + +out: + return reply; +} + +static void server_fini_one(libxl__gc *gc, struct vchan_client *client) +{ + pthread_mutex_lock(&vchan_client_mutex); + XEN_LIST_REMOVE(client, list); + pthread_mutex_unlock(&vchan_client_mutex); + + GC_FREE; + free(client->watch_ret); + free(client); +} + +static bool is_vchan_exist(libxl_ctx *ctx, char *watch_dir) +{ + char **dir = NULL; + unsigned int nb; + bool ret = false; + + dir = xs_directory(ctx->xsh, XBT_NULL, watch_dir, &nb); + if (dir) { + free(dir); + ret = true; + } + return ret; +} + +static void *client_thread(void *arg) +{ + struct vchan_client *client = arg; + + while (true) { + int ret; + /* + * libvchan uses garbage collector for processing requests, + * so we create a new one each time we process a packet and + * dispose it right away to prevent OOM. + */ + GC_INIT(client->ctx); + ret = vchan_process_command(gc, &client->info); + GC_FREE; + + if ((ret == ERROR_NOTFOUND) || (ret == ERROR_INVAL)) + continue; + if (ret < 0) + break; + } + vchan_fini_one(client->gc, client->info.state); + server_fini_one(client->gc, client); + return NULL; +} + +#define DEFAULT_THREAD_STACKSIZE (24 * 1024) +/* NetBSD doesn't have PTHREAD_STACK_MIN. */ +#ifndef PTHREAD_STACK_MIN +#define PTHREAD_STACK_MIN 0 +#endif + +#define READ_THREAD_STACKSIZE \ + ((DEFAULT_THREAD_STACKSIZE < PTHREAD_STACK_MIN) ? \ + PTHREAD_STACK_MIN : DEFAULT_THREAD_STACKSIZE) + +static bool init_client_thread(libxl__gc *gc, struct vchan_client *new_client) +{ + + sigset_t set, old_set; + pthread_attr_t attr; + static size_t stack_size; +#ifdef USE_DLSYM + size_t (*getsz)(pthread_attr_t *attr); +#endif + + if (pthread_attr_init(&attr) != 0) + return false; + if (!stack_size) { +#ifdef USE_DLSYM + getsz = dlsym(RTLD_DEFAULT, "__pthread_get_minstack"); + if (getsz) + stack_size = getsz(&attr); +#endif + if (stack_size < READ_THREAD_STACKSIZE) + stack_size = READ_THREAD_STACKSIZE; + } + if (pthread_attr_setstacksize(&attr, stack_size) != 0) { + pthread_attr_destroy(&attr); + return false; + } + + sigfillset(&set); + pthread_sigmask(SIG_SETMASK, &set, &old_set); + + if (pthread_create(&new_client->run_thread, &attr, client_thread, + new_client) != 0) { + pthread_sigmask(SIG_SETMASK, &old_set, NULL); + pthread_attr_destroy(&attr); + return false; + } + pthread_sigmask(SIG_SETMASK, &old_set, NULL); + pthread_attr_destroy(&attr); + + return true; +} + +static void init_new_client(libxl_ctx *ctx, libxl__gc *gc, + struct clients_list *list, char **watch_ret) +{ + struct vchan_client *new_client; + char *xs_path = watch_ret[XS_WATCH_PATH]; + + LOG(DEBUG, "New client at \"%s\"", xs_path); + + new_client = malloc(sizeof(*new_client)); + if (!new_client) { + LOGE(ERROR, "Failed to allocate new client at \"%s\"", xs_path); + return; + } + + memset(new_client, 0, sizeof(*new_client)); + + new_client->watch_ret = watch_ret; + new_client->watch_len = strlen(xs_path); + new_client->ctx = ctx; + /* + * Remember the GC of this client, so we can dispose its memory. + * Use it from now on. + */ + LIBXL_INIT_GC(new_client->gc[0], ctx); + + new_client->info.state = vchan_init_new_state(new_client->gc, DOM0_ID, + xs_path, true); + if (!(new_client->info.state)) { + LOGE(ERROR, "Failed to add new client at \"%s\"", xs_path); + server_fini_one(new_client->gc, new_client); + return; + } + + new_client->info.handle_request = pcid_handle_request; + new_client->info.prepare_response = pcid_prepare_response; + new_client->info.receive_buf_size = PCI_RECEIVE_BUFFER_SIZE; + new_client->info.max_buf_size = PCI_MAX_SIZE_RX_BUF; + + if (!init_client_thread(new_client->gc, new_client)) { + LOGE(ERROR, "Failed to create client's thread for \"%s\"", xs_path); + server_fini_one(new_client->gc, new_client); + return; + } + + pthread_mutex_lock(&vchan_client_mutex); + XEN_LIST_INSERT_HEAD(&vchan_clients, new_client, list); + pthread_mutex_unlock(&vchan_client_mutex); +} + +static void terminate_clients(void) +{ + struct vchan_client *client; + + pthread_mutex_lock(&vchan_client_mutex); + XEN_LIST_FOREACH(client, &vchan_clients, list) { + pthread_join(client->run_thread, NULL); + } + pthread_mutex_unlock(&vchan_client_mutex); +} + +int libxl_pcid_process(libxl_ctx *ctx) +{ + GC_INIT(ctx); + char *xs_path, *str; + char **watch_ret; + unsigned int watch_num; + libxl_domid domid; + int ret; + + pthread_mutex_init(&vchan_client_mutex, NULL); + + str = xs_read(ctx->xsh, 0, "domid", NULL); + if (!str) { + LOGE(ERROR, "Can't read own domid\n"); + ret = -ENOENT; + goto out; + } + + ret = sscanf(str, "%d", &domid); + free(str); + if (ret != 1) + { + LOGE(ERROR, "Own domid is not an integer\n"); + ret = -EINVAL; + goto out; + } + + xs_path = vchan_get_server_xs_path(gc, domid, PCID_SRV_NAME); + + /* Recreate the base folder: remove all leftovers. */ + ret = libxl__xs_rm_checked(gc, XBT_NULL, xs_path); + if (ret) + goto out; + + if (!xs_mkdir(CTX->xsh, XBT_NULL, xs_path)) + { + LOGE(ERROR, "xenstore mkdir failed: `%s'", xs_path); + ret = ERROR_FAIL; + goto out; + } + + /* Wait for vchan client to create a new UUID under the server's folder. */ + if (!xs_watch(CTX->xsh, xs_path, PCID_XS_TOKEN)) { + LOGE(ERROR, "xs_watch (%s) failed", xs_path); + ret = ERROR_FAIL; + goto out; + } + + while ((watch_ret = xs_read_watch(CTX->xsh, &watch_num))) { + struct vchan_client *client; + size_t len; + bool found; + + /* + * Any change under the base directory will fire an event, so we need + * to filter if this is indeed a new client or it is because vchan + * server creates nodes under its UUID. + * + * Never try to instantiate a vchan server right under xs_path. + */ + if (!strcmp(watch_ret[XS_WATCH_PATH], xs_path)) + continue; + + found = false; + len = strlen(watch_ret[XS_WATCH_PATH]); + + pthread_mutex_lock(&vchan_client_mutex); + XEN_LIST_FOREACH(client, &vchan_clients, list) { + str = client->watch_ret[XS_WATCH_PATH]; + + if (strstr(watch_ret[XS_WATCH_PATH], str)) { + /* + * Base path is a substring of the current path, so it can be: + * - a new node with different name, but starting with str + * - a subnode under str, so it will have '/' after str + * - same string + */ + if (len == client->watch_len) { + found = true; + break; + } + if (len > client->watch_len) { + if (watch_ret[XS_WATCH_PATH][client->watch_len] == '/') { + found = true; + break; + } + } + } + } + pthread_mutex_unlock(&vchan_client_mutex); + + if (!found && is_vchan_exist(ctx, watch_ret[XS_WATCH_PATH])) + init_new_client(ctx, gc, &vchan_clients, watch_ret); + } + + xs_unwatch(CTX->xsh, xs_path, PCID_XS_TOKEN); + +out: + terminate_clients(); + GC_FREE; + pthread_mutex_destroy(&vchan_client_mutex); + return ret; +} diff --git a/tools/xlpcid/Makefile b/tools/xlpcid/Makefile new file mode 100644 index 0000000000..238b6ef02f --- /dev/null +++ b/tools/xlpcid/Makefile @@ -0,0 +1,52 @@ +# +# tools/xlpcid/Makefile +# + +XEN_ROOT = $(CURDIR)/../.. +include $(XEN_ROOT)/tools/Rules.mk + +CFLAGS += -Wno-format-zero-length -Wmissing-declarations \ + -Wno-declaration-after-statement -Wformat-nonliteral +CFLAGS += -fPIC + +CFLAGS += $(PTHREAD_CFLAGS) +LDFLAGS += $(PTHREAD_LDFLAGS) + +CFLAGS_XLPCID += $(CFLAGS_libxenlight) +CFLAGS_XLPCID += $(CFLAGS_libxenutil) +CFLAGS_XLPCID += -Wshadow +CFLAGS_XLPCID += $(CFLAGS_libxenvchan) + +#XL_OBJS-$(CONFIG_X86) = xl_psr.o +XLPCID_OBJS = xlpcid.o $(XLPCID_OBJS-y) + +$(XLPCID_OBJS): CFLAGS += $(CFLAGS_libxentoollog) +$(XLPCID_OBJS): CFLAGS += $(CFLAGS_XLPCID) +$(XLPCID_OBJS): CFLAGS += -include $(XEN_ROOT)/tools/config.h # libxl_json.h needs it. + +.PHONY: all +all: xlpcid + +xlpcid: $(XLPCID_OBJS) + $(CC) $(LDFLAGS) -o $@ $(XLPCID_OBJS) $(LDLIBS_libxenutil) $(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) $(LDLIBS_libxenvchan) -lyajl $(APPEND_LDFLAGS) + +.PHONY: install +install: all + $(INSTALL_DIR) $(DESTDIR)$(sbindir) + $(INSTALL_DIR) $(DESTDIR)$(BASH_COMPLETION_DIR) + $(INSTALL_PROG) xlpcid $(DESTDIR)$(sbindir) + $(INSTALL_DATA) bash-completion $(DESTDIR)$(BASH_COMPLETION_DIR)/xlpcid + +.PHONY: uninstall +uninstall: + rm -f $(DESTDIR)$(BASH_COMPLETION_DIR)/xlpcid + rm -f $(DESTDIR)$(sbindir)/xlpcid + +.PHONY: clean +clean: + $(RM) *.o xlpcid $(DEPS_RM) + +distclean: clean + + +-include $(DEPS_INCLUDE) diff --git a/tools/xlpcid/bash-completion b/tools/xlpcid/bash-completion new file mode 100644 index 0000000000..578ef38b9f --- /dev/null +++ b/tools/xlpcid/bash-completion @@ -0,0 +1,20 @@ +# Copy this file to /etc/bash_completion.d/xl + +_xlpcid() +{ + local IFS=$'\n,' + + local cur opts xlpcid + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + xlpcid=xlpcid + + if [[ $COMP_CWORD == 1 ]] ; then + opts=`${xlpcid} help 2>/dev/null | sed '1,4d' | awk '/^ [^ ]/ {print $1}' | sed 's/$/ ,/g'` && COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + fi + + return 0 +} + +complete -F _xlpcid -o nospace -o default xlpcid diff --git a/tools/xlpcid/xlpcid.c b/tools/xlpcid/xlpcid.c new file mode 100644 index 0000000000..cb5804341d --- /dev/null +++ b/tools/xlpcid/xlpcid.c @@ -0,0 +1,213 @@ +/* + Pcid daemon that acts as a server for the client in the libxl PCI + + Copyright (C) 2021 EPAM Systems Inc. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; If not, see . + */ + +#define _GNU_SOURCE // required for strchrnul() + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +libxl_ctx *ctx; +xentoollog_logger_stdiostream *logger; +libxl_bitmap global_vm_affinity_mask; +libxl_bitmap global_hvm_affinity_mask; +libxl_bitmap global_pv_affinity_mask; +char *lockfile; +int logfile; + +static void help(void) +{ + fprintf(stderr, "Usage xlpcid [-ftTvp]\n") + fprintf(stderr, " --foreground - run in foreground mode\n"); + fprintf(stderr, " --pidfile - specify pid file\n"); + fprintf(stderr, " -t - Always use carriage-return-based overwriting\n"); + fprintf(stderr, " for displaying progress messages without scrolling\n"); + fprintf(stderr, " the screen. Without -t, this is done only if stderr is a tty.\n"); + fprintf(stderr, " -T - Include timestamps and pid of the xl process in output.\n"); + fprintf(stderr, " -v - Verbose\n"); +} + +static void xlpcid_ctx_alloc(void) +{ + if (libxl_ctx_alloc(&ctx, LIBXL_VERSION, 0, (xentoollog_logger*)logger)) { + fprintf(stderr, "cannot init xl context\n"); + exit(1); + } + + libxl_bitmap_init(&global_vm_affinity_mask); + libxl_bitmap_init(&global_hvm_affinity_mask); + libxl_bitmap_init(&global_pv_affinity_mask); +} + +static void xlpcid_ctx_free(void) +{ + libxl_bitmap_dispose(&global_pv_affinity_mask); + libxl_bitmap_dispose(&global_hvm_affinity_mask); + libxl_bitmap_dispose(&global_vm_affinity_mask); + if (ctx) { + libxl_ctx_free(ctx); + ctx = NULL; + } + if (logger) { + xtl_logger_destroy((xentoollog_logger*)logger); + logger = NULL; + } + if (lockfile) { + free(lockfile); + lockfile = NULL; + } +} + +static int do_daemonize(const char *name, const char *pidfile) +{ + char *fullname; + int nullfd, ret = 0; + + ret = libxl_create_logfile(ctx, name, &fullname); + if (ret) { + fprintf(stderr, "Failed to open logfile %s: %s", fullname, strerror(errno)); + exit(-1); + } + + logfile = open(fullname, O_WRONLY|O_CREAT|O_APPEND, 0644); + free(fullname); + assert(logfile >= 3); + + nullfd = open("/dev/null", O_RDONLY); + assert(nullfd >= 3); + + dup2(nullfd, 0); + dup2(logfile, 1); + dup2(logfile, 2); + + close(nullfd); + + if (daemon(0, 1)) { + perror("daemon"); + close(logfile); + return 1; + } + + if (pidfile) { + int fd = open(pidfile, O_RDWR | O_CREAT, S_IRUSR|S_IWUSR); + char *pid = NULL; + + if (fd == -1) { + perror("Unable to open pidfile"); + exit(1); + } + + if (asprintf(&pid, "%ld\n", (long)getpid()) == -1) { + perror("Formatting pid"); + exit(1); + } + + if (write(fd, pid, strlen(pid)) < 0) { + perror("Writing pid"); + exit(1); + } + + if (close(fd) < 0) { + perror("Closing pidfile"); + exit(1); + } + + free(pid); + } + + return ret; +} + +int main(int argc, char *argv[]) +{ + int opt = 0, daemonize = 1, ret = 0; + const char *pidfile = NULL; + unsigned int xtl_flags = 0; + bool progress_use_cr = 0; + bool timestamps = 0; + xentoollog_level minmsglevel = XTL_PROGRESS; + static const struct option opts[] = { + {"pidfile", 1, 0, 'p'}, + {"foreground", 0, 0, 'f'}, + {"help", 0, 0, 'h'}, + {0, 0, 0, 0} + }; + + while ((opt = getopt_long(argc, argv, "hftTvp:", opts, NULL)) != -1) + switch (opt) { + case 'f': + daemonize = 0; + break; + case 'p': + pidfile = optarg; + break; + case 't': + timestamps = 1; + break; + case 'T': + progress_use_cr = 1; + break; + case 'v': + if (minmsglevel > 0) + minmsglevel--; + break; + case 'h': + help(); + exit(1); + break; + default: + fprintf(stderr, "Unknown option '%c'\n", opt); + break; + } + + if (progress_use_cr) + xtl_flags |= XTL_STDIOSTREAM_PROGRESS_USE_CR; + if (timestamps) + xtl_flags |= XTL_STDIOSTREAM_SHOW_DATE | XTL_STDIOSTREAM_SHOW_PID; + logger = xtl_createlogger_stdiostream(stderr, minmsglevel, xtl_flags); + if (!logger) + exit(EXIT_FAILURE); + + xlpcid_ctx_alloc(); + + if (daemonize) { + ret = do_daemonize("xlpcid", pidfile); + if (ret) { + ret = (ret == 1) ? 0 : ret; + goto out_daemon; + } + } + + libxl_pcid_process(ctx); + +out_daemon: + xlpcid_ctx_free(); + exit(ret); +} From patchwork Thu Feb 9 15:55:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmytro Semenets X-Patchwork-Id: 13134828 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.xenproject.org (lists.xenproject.org [192.237.175.120]) (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 7D5BFC636D7 for ; Thu, 9 Feb 2023 15:56:51 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.492673.762354 (Exim 4.92) (envelope-from ) id 1pQ9Hn-0007a3-KQ; Thu, 09 Feb 2023 15:56:35 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 492673.762354; Thu, 09 Feb 2023 15:56:35 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pQ9Hn-0007Zd-Bo; Thu, 09 Feb 2023 15:56:35 +0000 Received: by outflank-mailman (input) for mailman id 492673; Thu, 09 Feb 2023 15:56:33 +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 1pQ9Hl-0006cl-6Z for xen-devel@lists.xenproject.org; Thu, 09 Feb 2023 15:56:33 +0000 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [2a00:1450:4864:20::62c]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 520f343b-a892-11ed-93b5-47a8fe42b414; Thu, 09 Feb 2023 16:56:31 +0100 (CET) Received: by mail-ej1-x62c.google.com with SMTP id p26so7634464ejx.13 for ; Thu, 09 Feb 2023 07:56:31 -0800 (PST) Received: from dsemenets-HP-EliteBook-850-G8-Notebook-PC.. ([91.219.254.72]) by smtp.gmail.com with ESMTPSA id v1-20020a170906b00100b0087862f45a29sm1027594ejy.174.2023.02.09.07.56.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Feb 2023 07:56:30 -0800 (PST) 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 Sender: "Xen-devel" X-Inumbo-ID: 520f343b-a892-11ed-93b5-47a8fe42b414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bmsCcMttxSYbw0pYCwcBt+l3AXaiynpdRsxaUXNJnFU=; b=XlEv70xEZXanGY83K7ttIqvpYBScKUNktKlPY0TNVpppood+1d70q32NBmaUYCy6xQ IJZJDYIGMDxk3Sz3N0xiPTxYAvvvPlpcE+sbvFM20PoyVmGKjD3uIteiWuP185Ik2zfz 4Gh/UxZIUiYLPx0kdolsndd04+fF6D1pf6pVlz3tK2TtVHteHdiktzYGbZu9m56augmo JKc3fEsqOzbnnbmXS2i33e8W+vSigVQlkGM3KANhXkeYST6ucrbNjfS3oo8Nd36NqM9m mlQqVuWBScq3/QbX7W08CGCMqRG8WYCmQNrJI38yI+2LIL9NSRW0i39EyAFFsJlsE/pC PTWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bmsCcMttxSYbw0pYCwcBt+l3AXaiynpdRsxaUXNJnFU=; b=szxVtd4RypTbUejWpQEBk+3DALJfn/xz/ILY6Q7Y3fzEgiaazQiDJzxW3Ekj0woPFX pHjnhxOdc/ceBDcAGgymjKhashFkB5lUiAeGqT/LoIfKDNnQVJ4/BlVoDszvmKhoXAWL y2ofoRIlXhllCv0WLmb6zQlGJdJM3CJQooEH7REkAeKz7kfe30cCINQcRrM6xKCgOSzX ac+ObPtchRg8QI4qS+NkXAEc8xhRtC4nKWs9JW9RmAhqspbCi2y8HI6xb6QAIOTWgsbd G5D8fwAbls/23WIHWMVID4WYgNGwnwsnFsowuEcD6TVD8YWEvmTdz9Y8UItxs2UA4n58 lHoQ== X-Gm-Message-State: AO0yUKU+lyfDBSmoDgYSw3APkRmSep9ewYQs4ZVbq//2QKYSF733JT1X GV2hqKsXEQ6f0KLcYsbASLodubuegtQntA== X-Google-Smtp-Source: AK7set9WOPbNmChLMlaNwilvjNpmtQqdh0PqIIhGzFj9tr3YfvIYFxIiBolR11n0JK/CIL1A6ly3XA== X-Received: by 2002:a17:906:5185:b0:8ae:ea08:3842 with SMTP id y5-20020a170906518500b008aeea083842mr5504778ejk.7.1675958190806; Thu, 09 Feb 2023 07:56:30 -0800 (PST) From: dmitry.semenets@gmail.com To: xen-devel@lists.xenproject.org Cc: Dmytro Semenets , Wei Liu , Anthony PERARD , Juergen Gross , Oleksandr Andrushchenko Subject: [RFC PATCH v4 4/9] tools/libs/light: pcid: implement list_assignable command Date: Thu, 9 Feb 2023 17:55:58 +0200 Message-Id: <20230209155604.2753219-5-dmitry.semenets@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209155604.2753219-1-dmitry.semenets@gmail.com> References: <20230209155604.2753219-1-dmitry.semenets@gmail.com> MIME-Version: 1.0 From: Dmytro Semenets Signed-off-by: Oleksandr Andrushchenko Signed-off-by: Dmytro Semenets --- tools/include/xen-pcid.h | 19 ++++++++++++ tools/libs/light/libxl_pci.c | 54 ++++++++++++++++++++++----------- tools/libs/light/libxl_pcid.c | 56 ++++++++++++++++++++++++++++++----- 3 files changed, 103 insertions(+), 26 deletions(-) diff --git a/tools/include/xen-pcid.h b/tools/include/xen-pcid.h index 098118a0c3..6a5724363d 100644 --- a/tools/include/xen-pcid.h +++ b/tools/include/xen-pcid.h @@ -80,6 +80,25 @@ #define PCID_SBDF_FMT "%04x:%02x:%02x.%01x" +/* + ******************************************************************************* + * List assignable devices + * + * This command lists PCI devices that can be passed through to a guest domain. + * + * Request (see other mandatory fields above): + * - "cmd" field of the request must be set to "list_assignable". + * + * Response (see other mandatory fields above): + * - "resp" field of the response must be set to "list_assignable". + * Command specific response data: + * +-------------+--------------+----------------------------------------------+ + * | devices | list | List of of pci_device objects | + * +-------------+--------------+----------------------------------------------+ + */ +#define PCID_CMD_LIST_ASSIGNABLE "list_assignable" +#define PCID_MSG_FIELD_DEVICES "devices" + int libxl_pcid_process(libxl_ctx *ctx); #endif /* XEN_PCID_H */ diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c index 3d9ed0d50c..c4b04ccb92 100644 --- a/tools/libs/light/libxl_pci.c +++ b/tools/libs/light/libxl_pci.c @@ -29,6 +29,18 @@ #define PCI_BDF_XSPATH "%04x-%02x-%02x-%01x" #define PCI_PT_QDEV_ID "pci-pt-%02x_%02x.%01x" +static int process_list_assignable(libxl__gc *gc, + const libxl__json_object *response, + libxl__json_object **result) +{ + *result = (libxl__json_object *)libxl__json_map_get(PCID_MSG_FIELD_DEVICES, + response, JSON_ARRAY); + if (!*result) + return ERROR_INVAL; + + return 0; +} + static int pci_handle_response(libxl__gc *gc, const libxl__json_object *response, libxl__json_object **result) @@ -68,6 +80,9 @@ static int pci_handle_response(libxl__gc *gc, command_name = command_obj->u.string; LOG(DEBUG, "command: %s", command_name); + if (strcmp(command_name, PCID_CMD_LIST_ASSIGNABLE) == 0) + ret = process_list_assignable(gc, response, result); + return ret; } @@ -124,8 +139,7 @@ static char *pci_prepare_request(libxl__gc *gc, yajl_gen gen, char *cmd, return request; } -struct vchan_info *pci_vchan_get_client(libxl__gc *gc); -struct vchan_info *pci_vchan_get_client(libxl__gc *gc) +static struct vchan_info *pci_vchan_get_client(libxl__gc *gc) { struct vchan_info *vchan; @@ -147,8 +161,7 @@ out: return vchan; } -void pci_vchan_free(libxl__gc *gc, struct vchan_info *vchan); -void pci_vchan_free(libxl__gc *gc, struct vchan_info *vchan) +static void pci_vchan_free(libxl__gc *gc, struct vchan_info *vchan) { vchan_fini_one(gc, vchan->state); } @@ -561,26 +574,29 @@ libxl_device_pci *libxl_device_pci_assignable_list(libxl_ctx *ctx, int *num) { GC_INIT(ctx); libxl_device_pci *pcis = NULL, *new; - struct dirent *de; - DIR *dir; + struct vchan_info *vchan; + libxl__json_object *result, *dev_obj; + int i; *num = 0; - dir = opendir(SYSFS_PCIBACK_DRIVER); - if (NULL == dir) { - if (errno == ENOENT) { - LOG(ERROR, "Looks like pciback driver not loaded"); - } else { - LOGE(ERROR, "Couldn't open %s", SYSFS_PCIBACK_DRIVER); - } + vchan = pci_vchan_get_client(gc); + if (!vchan) goto out; - } - while((de = readdir(dir))) { + result = vchan_send_command(gc, vchan, PCID_CMD_LIST_ASSIGNABLE, NULL); + if (!result) + goto vchan_free; + + for (i = 0; (dev_obj = libxl__json_array_get(result, i)); i++) { + const char *sbdf_str = libxl__json_object_get_string(dev_obj); unsigned int dom, bus, dev, func; - char *name; + const char *name; + + if (!sbdf_str) + continue; - if (sscanf(de->d_name, PCI_BDF, &dom, &bus, &dev, &func) != 4) + if (sscanf(sbdf_str, PCID_SBDF_FMT, &dom, &bus, &dev, &func) != 4) continue; new = realloc(pcis, ((*num) + 1) * sizeof(*new)); @@ -602,7 +618,9 @@ libxl_device_pci *libxl_device_pci_assignable_list(libxl_ctx *ctx, int *num) (*num)++; } - closedir(dir); +vchan_free: + pci_vchan_free(gc, vchan); + out: GC_FREE; return pcis; diff --git a/tools/libs/light/libxl_pcid.c b/tools/libs/light/libxl_pcid.c index 2ce8f1504d..8dfccc439d 100644 --- a/tools/libs/light/libxl_pcid.c +++ b/tools/libs/light/libxl_pcid.c @@ -84,6 +84,41 @@ static int make_error_reply(libxl__gc *gc, yajl_gen gen, char *desc, return 0; } +static int process_list_assignable(libxl__gc *gc, yajl_gen gen, + char *command_name, + const struct libxl__json_object *request, + struct libxl__json_object **response) +{ + struct dirent *de; + DIR *dir = NULL; + + dir = opendir(SYSFS_PCI_DEV); + if (dir == NULL) { + make_error_reply(gc, gen, strerror(errno), command_name); + return ERROR_FAIL; + } + + libxl__yajl_gen_asciiz(gen, PCID_MSG_FIELD_DEVICES); + + *response = libxl__json_object_alloc(gc, JSON_ARRAY); + + while ((de = readdir(dir))) { + unsigned int dom, bus, dev, func; + + if (sscanf(de->d_name, PCID_SBDF_FMT, &dom, &bus, &dev, &func) != 4) + continue; + + struct libxl__json_object *node = + libxl__json_object_alloc(gc, JSON_STRING); + node->u.string = de->d_name; + flexarray_append((*response)->u.array, node); + } + + closedir(dir); + + return 0; +} + static int pcid_handle_request(libxl__gc *gc, yajl_gen gen, const libxl__json_object *request) { @@ -104,14 +139,19 @@ static int pcid_handle_request(libxl__gc *gc, yajl_gen gen, command_name = command_obj->u.string; - /* - * This is an unsupported command: make a reply and proceed over - * the error path. - */ - ret = make_error_reply(gc, gen, "Unsupported command", - command_name); - if (!ret) - ret = ERROR_NOTFOUND; + if (strcmp(command_name, PCID_CMD_LIST_ASSIGNABLE) == 0) + ret = process_list_assignable(gc, gen, command_name, + request, &command_response); + else { + /* + * This is an unsupported command: make a reply and proceed over + * the error path. + */ + ret = make_error_reply(gc, gen, "Unsupported command", + command_name); + if (!ret) + ret = ERROR_NOTFOUND; + } if (ret) { /* From patchwork Thu Feb 9 15:55:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmytro Semenets X-Patchwork-Id: 13134832 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.xenproject.org (lists.xenproject.org [192.237.175.120]) (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 38F1BC636D7 for ; Thu, 9 Feb 2023 15:56:54 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.492678.762375 (Exim 4.92) (envelope-from ) id 1pQ9Hp-00081W-Aj; Thu, 09 Feb 2023 15:56:37 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 492678.762375; Thu, 09 Feb 2023 15:56:37 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pQ9Ho-0007yN-VO; Thu, 09 Feb 2023 15:56:36 +0000 Received: by outflank-mailman (input) for mailman id 492678; Thu, 09 Feb 2023 15:56:35 +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 1pQ9Hn-0006cl-8z for xen-devel@lists.xenproject.org; Thu, 09 Feb 2023 15:56:35 +0000 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [2a00:1450:4864:20::630]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 52e438b2-a892-11ed-93b5-47a8fe42b414; Thu, 09 Feb 2023 16:56:32 +0100 (CET) Received: by mail-ej1-x630.google.com with SMTP id jg8so7713077ejc.6 for ; Thu, 09 Feb 2023 07:56:32 -0800 (PST) Received: from dsemenets-HP-EliteBook-850-G8-Notebook-PC.. ([91.219.254.72]) by smtp.gmail.com with ESMTPSA id v1-20020a170906b00100b0087862f45a29sm1027594ejy.174.2023.02.09.07.56.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Feb 2023 07:56:31 -0800 (PST) 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 Sender: "Xen-devel" X-Inumbo-ID: 52e438b2-a892-11ed-93b5-47a8fe42b414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=iDSIcL+mVhfPXUiOzfh6UyHDqJq2g2EiKPGtIhsGJug=; b=Tsv1J0SIn2AQiBfOIN4Zx1VU1h6vO/6e70o2ZhUZs1UiNXjuI/zPs7jf4dClIGaS8t l85GPQopTZuCo2FHS+b1lalYeHUlQ4ie6cA+jlbzjVs+Lb3duAjyldyKv1SFo5wWoXk2 Pw//cSTqS+xhdEWjGLqNMfRPjX5JmZiQQbF861Ehqu7BWobMCTrwlBFpha8ZHOPoMWmX PN3YF+W5vS8S24dHFVllwRDKLunUzuk8BYmK20XY4Xtm1O7Od3Clx4Tqi3V9bPHd0vnU SiJVvMLv2Ae3QpDVNauzbWu+g7v3swyVM9oafKHSjr/dE5LNa7TW7Fg2Ipv4jAc1IUTh 3f9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iDSIcL+mVhfPXUiOzfh6UyHDqJq2g2EiKPGtIhsGJug=; b=waTH0u9x/p8oupDs6YEUJ8G8AnlUR4F6+6I3orO2Kgl0czTcU5ABpp4YWZ9QTs3lfq 5dte2vA8imvWI5KgYl7ztQvDCFWGe20vXZ7JJI/a+wL3SWzTXtTxObDWsUoQ5LKWsZa3 mhhakWVldEfaphVgSt5+sGKP6fBiGfxpT86Rcb0ZdutdzYaSBST2mjkQ6cRqELhqXI4Y wDmz7hzJw2mfmaYpTMLC2qAeZa1E+p5wCC27tOPYT8jxeWXhuiUSSHRld8/8fmKMQzLD gCT13grUtCb9nBhRdo/RgGSEjOHoME65n+ocoJWnMpwEpDEkzmnJ7oJiBtaUtNhkLQ0F 5gfQ== X-Gm-Message-State: AO0yUKU/oQ6fO+xJ5n7nNvWTZtu1fQXqBUkf4GikTVRyTrRlKJ35I9FA LJUqpM116qy1Zu46xQS+oQiPMaTk0OzCfA== X-Google-Smtp-Source: AK7set9QCgZKoZwqmEPnbSoTdDC1nIasulaWiBEwtR3KAzfx+GskGsW+KyFfVuWD/lcbAarUsjLZeg== X-Received: by 2002:a17:906:5307:b0:88f:1255:59c with SMTP id h7-20020a170906530700b0088f1255059cmr9552033ejo.1.1675958191909; Thu, 09 Feb 2023 07:56:31 -0800 (PST) From: dmitry.semenets@gmail.com To: xen-devel@lists.xenproject.org Cc: Volodymyr Babchuk , Wei Liu , Anthony PERARD , Juergen Gross Subject: [RFC PATCH v4 5/9] tools/light: pci: move assign/revert logic to pcid Date: Thu, 9 Feb 2023 17:55:59 +0200 Message-Id: <20230209155604.2753219-6-dmitry.semenets@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209155604.2753219-1-dmitry.semenets@gmail.com> References: <20230209155604.2753219-1-dmitry.semenets@gmail.com> MIME-Version: 1.0 From: Volodymyr Babchuk Implement commands MAKE_ASSIGNABLE and REVERT_ASSIGNABLE in pcid in the same way as they were implemented in libxl_pci.c Replace original logic in libxl_pci.c by calling appropriate functions from pcid. This is quite huge patch, as lots of code were moved from lixbl_pci.c to libxl_pcid.c. Signed-off-by: Volodymyr Babchuk --- tools/include/xen-pcid.h | 38 ++++ tools/libs/light/libxl_pci.c | 292 +++++-------------------- tools/libs/light/libxl_pcid.c | 396 ++++++++++++++++++++++++++++++++++ 3 files changed, 492 insertions(+), 234 deletions(-) diff --git a/tools/include/xen-pcid.h b/tools/include/xen-pcid.h index 6a5724363d..5e96b8de6e 100644 --- a/tools/include/xen-pcid.h +++ b/tools/include/xen-pcid.h @@ -99,6 +99,44 @@ #define PCID_CMD_LIST_ASSIGNABLE "list_assignable" #define PCID_MSG_FIELD_DEVICES "devices" +/* + ******************************************************************************* + * Make device assignable + * + * This command makes given device assignable by ensuring that OS + * will not try to access it. + * + * Request (see other mandatory fields above): + * - "cmd" field of the request must be set to "make_assignable". + * - "sbdf" SBDF of the device in format defined by PCID_SBDF_FMT. + * - "rebind" = true if daemon needs to save original driver name, + * so device later can be rebound back. + * + * Response (see other mandatory fields above): + * - "resp" field of the response must be set to "make_assignable". + */ +#define PCID_CMD_MAKE_ASSIGNABLE "make_assignable" +#define PCID_MSG_FIELD_REBIND "rebind" + +/* + ******************************************************************************* + * Revert device from assignable state + * + * This command reverts effect of "make_assignable" command. Basically, + * now device can be used by OS again. + * + * Request (see other mandatory fields above): + * - "cmd" field of the request must be set to "revert_assignable". + * - "sbdf" SBDF of the device in format defined by PCID_SBDF_FMT. + * - "rebind" = true if daemon needs to rebind device back to it's + * original driver, which name was saved by "make_assignable" command + * + * Response (see other mandatory fields above): + * - "resp" field of the response must be set to "revert_assignable". + */ +#define PCID_CMD_REVERT_ASSIGNABLE "revert_assignable" + + int libxl_pcid_process(libxl_ctx *ctx); #endif /* XEN_PCID_H */ diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c index c4b04ccb92..b3dfd95fd3 100644 --- a/tools/libs/light/libxl_pci.c +++ b/tools/libs/light/libxl_pci.c @@ -82,6 +82,10 @@ static int pci_handle_response(libxl__gc *gc, if (strcmp(command_name, PCID_CMD_LIST_ASSIGNABLE) == 0) ret = process_list_assignable(gc, response, result); + else if (strcmp(command_name, PCID_CMD_MAKE_ASSIGNABLE) == 0) + *result = libxl__json_object_alloc(gc, JSON_NULL); + else if (strcmp(command_name, PCID_CMD_REVERT_ASSIGNABLE) == 0) + *result = libxl__json_object_alloc(gc, JSON_NULL); return ret; } @@ -636,44 +640,6 @@ void libxl_device_pci_assignable_list_free(libxl_device_pci *list, int num) free(list); } -/* Unbind device from its current driver, if any. If driver_path is non-NULL, - * store the path to the original driver in it. */ -static int sysfs_dev_unbind(libxl__gc *gc, libxl_device_pci *pci, - char **driver_path) -{ - char * spath, *dp = NULL; - struct stat st; - - spath = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/driver", - pci->domain, - pci->bus, - pci->dev, - pci->func); - if ( !lstat(spath, &st) ) { - /* Find the canonical path to the driver. */ - dp = libxl__zalloc(gc, PATH_MAX); - dp = realpath(spath, dp); - if ( !dp ) { - LOGE(ERROR, "realpath() failed"); - return -1; - } - - LOG(DEBUG, "Driver re-plug path: %s", dp); - - /* Unbind from the old driver */ - spath = GCSPRINTF("%s/unbind", dp); - if ( sysfs_write_bdf(gc, spath, pci) < 0 ) { - LOGE(ERROR, "Couldn't unbind device"); - return -1; - } - } - - if ( driver_path ) - *driver_path = dp; - - return 0; -} - static uint16_t sysfs_dev_get_vendor(libxl__gc *gc, libxl_device_pci *pci) { char *pci_device_vendor_path = @@ -785,49 +751,6 @@ bool libxl__is_igd_vga_passthru(libxl__gc *gc, return false; } -/* - * A brief comment about slots. I don't know what slots are for; however, - * I have by experimentation determined: - * - Before a device can be bound to pciback, its BDF must first be listed - * in pciback/slots - * - The way to get the BDF listed there is to write BDF to - * pciback/new_slot - * - Writing the same BDF to pciback/new_slot is not idempotent; it results - * in two entries of the BDF in pciback/slots - * It's not clear whether having two entries in pciback/slots is a problem - * or not. Just to be safe, this code does the conservative thing, and - * first checks to see if there is a slot, adding one only if one does not - * already exist. - */ - -/* Scan through /sys/.../pciback/slots looking for pci's BDF */ -static int pciback_dev_has_slot(libxl__gc *gc, libxl_device_pci *pci) -{ - FILE *f; - int rc = 0; - unsigned dom, bus, dev, func; - - f = fopen(SYSFS_PCIBACK_DRIVER"/slots", "r"); - - if (f == NULL) { - LOGE(ERROR, "Couldn't open %s", SYSFS_PCIBACK_DRIVER"/slots"); - return ERROR_FAIL; - } - - while (fscanf(f, "%x:%x:%x.%d\n", &dom, &bus, &dev, &func) == 4) { - if (dom == pci->domain - && bus == pci->bus - && dev == pci->dev - && func == pci->func) { - rc = 1; - goto out; - } - } -out: - fclose(f); - return rc; -} - static int pciback_dev_is_assigned(libxl__gc *gc, libxl_device_pci *pci) { char * spath; @@ -856,133 +779,34 @@ static int pciback_dev_is_assigned(libxl__gc *gc, libxl_device_pci *pci) return -1; } -static int pciback_dev_assign(libxl__gc *gc, libxl_device_pci *pci) -{ - int rc; - - if ( (rc = pciback_dev_has_slot(gc, pci)) < 0 ) { - LOGE(ERROR, "Error checking for pciback slot"); - return ERROR_FAIL; - } else if (rc == 0) { - if ( sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER"/new_slot", - pci) < 0 ) { - LOGE(ERROR, "Couldn't bind device to pciback!"); - return ERROR_FAIL; - } - } - - if ( sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER"/bind", pci) < 0 ) { - LOGE(ERROR, "Couldn't bind device to pciback!"); - return ERROR_FAIL; - } - return 0; -} - -static int pciback_dev_unassign(libxl__gc *gc, libxl_device_pci *pci) -{ - /* Remove from pciback */ - if ( sysfs_dev_unbind(gc, pci, NULL) < 0 ) { - LOG(ERROR, "Couldn't unbind device!"); - return ERROR_FAIL; - } - - /* Remove slot if necessary */ - if ( pciback_dev_has_slot(gc, pci) > 0 ) { - if ( sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER"/remove_slot", - pci) < 0 ) { - LOGE(ERROR, "Couldn't remove pciback slot"); - return ERROR_FAIL; - } - } - return 0; -} - static int libxl__device_pci_assignable_add(libxl__gc *gc, libxl_device_pci *pci, int rebind) { libxl_ctx *ctx = libxl__gc_owner(gc); - unsigned dom, bus, dev, func; - char *spath, *driver_path = NULL; - const char *name; + struct vchan_info *vchan; int rc; - struct stat st; - - /* Local copy for convenience */ - dom = pci->domain; - bus = pci->bus; - dev = pci->dev; - func = pci->func; - name = pci->name; - - /* Sanitise any name that is set */ - if (name) { - unsigned int i, n = strlen(name); + libxl__json_object *args, *result; - if (n > 64) { /* Reasonable upper bound on name length */ - LOG(ERROR, "Name too long"); - return ERROR_FAIL; - } - - for (i = 0; i < n; i++) { - if (!isgraph(name[i])) { - LOG(ERROR, "Names may only include printable characters"); - return ERROR_FAIL; - } - } - } - - /* See if the device exists */ - spath = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF, dom, bus, dev, func); - if ( lstat(spath, &st) ) { - LOGE(ERROR, "Couldn't lstat %s", spath); - return ERROR_FAIL; - } - - /* Check to see if it's already assigned to pciback */ - rc = pciback_dev_is_assigned(gc, pci); - if ( rc < 0 ) { - return ERROR_FAIL; - } - if ( rc ) { - LOG(WARN, PCI_BDF" already assigned to pciback", dom, bus, dev, func); - goto name; + vchan = pci_vchan_get_client(gc); + if (!vchan) { + rc = ERROR_NOT_READY; + goto out; } - /* Check to see if there's already a driver that we need to unbind from */ - if ( sysfs_dev_unbind(gc, pci, &driver_path ) ) { - LOG(ERROR, "Couldn't unbind "PCI_BDF" from driver", - dom, bus, dev, func); - return ERROR_FAIL; - } + args = libxl__vchan_start_args(gc); - /* Store driver_path for rebinding to dom0 */ - if ( rebind ) { - if ( driver_path ) { - pci_info_xs_write(gc, pci, "driver_path", driver_path); - } else if ( (driver_path = - pci_info_xs_read(gc, pci, "driver_path")) != NULL ) { - LOG(INFO, PCI_BDF" not bound to a driver, will be rebound to %s", - dom, bus, dev, func, driver_path); - } else { - LOG(WARN, PCI_BDF" not bound to a driver, will not be rebound.", - dom, bus, dev, func); - } - } else { - pci_info_xs_remove(gc, pci, "driver_path"); - } + libxl__vchan_arg_add_string(gc, args, PCID_MSG_FIELD_SBDF, + GCSPRINTF(PCID_SBDF_FMT, pci->domain, + pci->bus, pci->dev, pci->func)); + libxl__vchan_arg_add_bool(gc, args, PCID_MSG_FIELD_REBIND, rebind); - if ( pciback_dev_assign(gc, pci) ) { - LOG(ERROR, "Couldn't bind device to pciback!"); - return ERROR_FAIL; + result = vchan_send_command(gc, vchan, PCID_CMD_MAKE_ASSIGNABLE, args); + if (!result) { + rc = ERROR_FAIL; + goto vchan_free; } -name: - if (name) - pci_info_xs_write(gc, pci, "name", name); - else - pci_info_xs_remove(gc, pci, "name"); - /* * DOMID_IO is just a sentinel domain, without any actual mappings, * so always pass XEN_DOMCTL_DEV_RDM_RELAXED to avoid assignment being @@ -990,12 +814,15 @@ name: */ rc = xc_assign_device(ctx->xch, DOMID_IO, pci_encode_bdf(pci), XEN_DOMCTL_DEV_RDM_RELAXED); - if ( rc < 0 ) { - LOG(ERROR, "failed to quarantine "PCI_BDF, dom, bus, dev, func); - return ERROR_FAIL; - } + if ( rc < 0 ) + LOG(ERROR, "failed to quarantine "PCI_BDF, pci->domain, pci->bus, + pci->dev, pci->func); - return 0; +vchan_free: + pci_vchan_free(gc, vchan); + +out: + return rc; } static int name2bdf(libxl__gc *gc, libxl_device_pci *pci) @@ -1038,13 +865,8 @@ static int libxl__device_pci_assignable_remove(libxl__gc *gc, { libxl_ctx *ctx = libxl__gc_owner(gc); int rc; - char *driver_path; - - /* If the device is named then we need to look up the BDF */ - if (pci->name) { - rc = name2bdf(gc, pci); - if (rc) return rc; - } + struct vchan_info *vchan; + libxl__json_object *args, *temp_obj, *result; /* De-quarantine */ rc = xc_deassign_device(ctx->xch, DOMID_IO, pci_encode_bdf(pci)); @@ -1054,41 +876,43 @@ static int libxl__device_pci_assignable_remove(libxl__gc *gc, return ERROR_FAIL; } - /* Unbind from pciback */ - if ( (rc = pciback_dev_is_assigned(gc, pci)) < 0 ) { - return ERROR_FAIL; - } else if ( rc ) { - pciback_dev_unassign(gc, pci); - } else { - LOG(WARN, "Not bound to pciback"); + vchan = pci_vchan_get_client(gc); + if (!vchan) { + rc = ERROR_NOT_READY; + goto out; } - /* Rebind if necessary */ - driver_path = pci_info_xs_read(gc, pci, "driver_path"); + args = libxl__json_object_alloc(gc, JSON_MAP); + temp_obj = libxl__json_object_alloc(gc, JSON_STRING); + if (!temp_obj) { + rc = ERROR_NOMEM; + goto vchan_free; + } + temp_obj->u.string = GCSPRINTF(PCID_SBDF_FMT, pci->domain, pci->bus, + pci->dev, pci->func); + flexarray_append_pair(args->u.map, PCID_MSG_FIELD_SBDF, temp_obj); - if ( driver_path ) { - if ( rebind ) { - LOG(INFO, "Rebinding to driver at %s", driver_path); + args = libxl__json_object_alloc(gc, JSON_MAP); + temp_obj = libxl__json_object_alloc(gc, JSON_BOOL); + if (!temp_obj) { + rc = ERROR_NOMEM; + goto vchan_free; + } - if ( sysfs_write_bdf(gc, - GCSPRINTF("%s/bind", driver_path), - pci) < 0 ) { - LOGE(ERROR, "Couldn't bind device to %s", driver_path); - return -1; - } + temp_obj->u.b = rebind; + flexarray_append_pair(args->u.map, PCID_MSG_FIELD_REBIND, temp_obj); - pci_info_xs_remove(gc, pci, "driver_path"); - } - } else { - if ( rebind ) { - LOG(WARN, - "Couldn't find path for original driver; not rebinding"); - } + result = vchan_send_command(gc, vchan, PCID_CMD_REVERT_ASSIGNABLE, args); + if (!result) { + rc = ERROR_FAIL; + goto vchan_free; } - pci_info_xs_remove(gc, pci, "name"); +vchan_free: + pci_vchan_free(gc, vchan); - return 0; +out: + return rc; } int libxl_device_pci_assignable_add(libxl_ctx *ctx, libxl_device_pci *pci, diff --git a/tools/libs/light/libxl_pcid.c b/tools/libs/light/libxl_pcid.c index 8dfccc439d..5e1e41feb5 100644 --- a/tools/libs/light/libxl_pcid.c +++ b/tools/libs/light/libxl_pcid.c @@ -38,6 +38,8 @@ #define DOM0_ID 0 +#define PCI_BDF "%04x:%02x:%02x.%01x" + struct vchan_client { XEN_LIST_ENTRY(struct vchan_client) list; @@ -119,6 +121,394 @@ static int process_list_assignable(libxl__gc *gc, yajl_gen gen, return 0; } +static int pciback_dev_is_assigned(libxl__gc *gc, unsigned int domain, + unsigned int bus, unsigned int dev, + unsigned int func) +{ + char * spath; + int rc; + struct stat st; + + if (access(SYSFS_PCIBACK_DRIVER, F_OK) < 0) { + if (errno == ENOENT) { + LOG(ERROR, "Looks like pciback driver is not loaded"); + } else { + LOGE(ERROR, "Can't access "SYSFS_PCIBACK_DRIVER); + } + return -1; + } + + spath = GCSPRINTF(SYSFS_PCIBACK_DRIVER"/"PCI_BDF, + domain, bus, dev, func); + rc = lstat(spath, &st); + + if (rc == 0) + return 1; + if (rc < 0 && errno == ENOENT) + return 0; + LOGE(ERROR, "Accessing %s", spath); + return -1; +} + +#define PCID_INFO_PATH "pcid" +#define PCID_BDF_XSPATH "%04x-%02x-%02x-%01x" + +static char *pcid_info_xs_path(libxl__gc *gc, unsigned int domain, + unsigned int bus, unsigned int dev, + unsigned int func, const char *node) +{ + return node ? + GCSPRINTF(PCID_INFO_PATH"/"PCID_BDF_XSPATH"/%s", + domain, bus, dev, func, node) : + GCSPRINTF(PCID_INFO_PATH"/"PCID_BDF_XSPATH, + domain, bus, dev, func); +} + + +static int pcid_info_xs_write(libxl__gc *gc, unsigned int domain, + unsigned int bus, unsigned int dev, + unsigned int func, const char *node, + const char *val) +{ + char *path = pcid_info_xs_path(gc, domain, bus, dev, func, node); + int rc = libxl__xs_printf(gc, XBT_NULL, path, "%s", val); + + if (rc) LOGE(WARN, "Write of %s to node %s failed.", val, path); + + return rc; +} + +static char *pcid_info_xs_read(libxl__gc *gc, unsigned int domain, + unsigned int bus, unsigned int dev, + unsigned int func, const char *node) +{ + char *path = pcid_info_xs_path(gc, domain, bus, dev, func, node); + + return libxl__xs_read(gc, XBT_NULL, path); +} + +static void pcid_info_xs_remove(libxl__gc *gc, unsigned int domain, + unsigned int bus, unsigned int dev, + unsigned int func, const char *node) +{ + char *path = pcid_info_xs_path(gc, domain, bus, dev, func, node); + libxl_ctx *ctx = libxl__gc_owner(gc); + + /* Remove the xenstore entry */ + xs_rm(ctx->xsh, XBT_NULL, path); +} + + +/* Write the standard BDF into the sysfs path given by sysfs_path. */ +static int sysfs_write_bdf(libxl__gc *gc, const char * sysfs_path, + unsigned int domain, unsigned int bus, + unsigned int dev, unsigned int func) +{ + int rc, fd; + char *buf; + + fd = open(sysfs_path, O_WRONLY); + if (fd < 0) { + LOGE(ERROR, "Couldn't open %s", sysfs_path); + return ERROR_FAIL; + } + + buf = GCSPRINTF(PCI_BDF, domain, bus, dev, func); + rc = write(fd, buf, strlen(buf)); + /* Annoying to have two if's, but we need the errno */ + if (rc < 0) + LOGE(ERROR, "write to %s returned %d", sysfs_path, rc); + close(fd); + + if (rc < 0) + return ERROR_FAIL; + + return 0; +} + + +/* Unbind device from its current driver, if any. If driver_path is non-NULL, + * store the path to the original driver in it. */ +static int sysfs_dev_unbind(libxl__gc *gc, unsigned int domain, + unsigned int bus, unsigned int dev, + unsigned int func, + char **driver_path) +{ + char * spath, *dp = NULL; + struct stat st; + + spath = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/driver", + domain, bus, dev, func); + if (!lstat(spath, &st)) { + /* Find the canonical path to the driver. */ + dp = libxl__zalloc(gc, PATH_MAX); + dp = realpath(spath, dp); + if ( !dp ) { + LOGE(ERROR, "realpath() failed"); + return -1; + } + + LOG(DEBUG, "Driver re-plug path: %s", dp); + + /* Unbind from the old driver */ + spath = GCSPRINTF("%s/unbind", dp); + if (sysfs_write_bdf(gc, spath, domain, bus, dev, func) < 0) { + LOGE(ERROR, "Couldn't unbind device"); + return -1; + } + } + + if (driver_path) + *driver_path = dp; + + return 0; +} + +/* + * A brief comment about slots. I don't know what slots are for; however, + * I have by experimentation determined: + * - Before a device can be bound to pciback, its BDF must first be listed + * in pciback/slots + * - The way to get the BDF listed there is to write BDF to + * pciback/new_slot + * - Writing the same BDF to pciback/new_slot is not idempotent; it results + * in two entries of the BDF in pciback/slots + * It's not clear whether having two entries in pciback/slots is a problem + * or not. Just to be safe, this code does the conservative thing, and + * first checks to see if there is a slot, adding one only if one does not + * already exist. + */ + +/* Scan through /sys/.../pciback/slots looking for pci's BDF */ +static int pciback_dev_has_slot(libxl__gc *gc, unsigned int domain, + unsigned int bus, unsigned int dev, + unsigned int func) +{ + FILE *f; + int rc = 0; + unsigned s_domain, s_bus, s_dev, s_func; + + f = fopen(SYSFS_PCIBACK_DRIVER"/slots", "r"); + + if (f == NULL) { + LOGE(ERROR, "Couldn't open %s", SYSFS_PCIBACK_DRIVER"/slots"); + return ERROR_FAIL; + } + + while (fscanf(f, "%x:%x:%x.%d\n", + &s_domain, &s_bus, &s_dev, &s_func) == 4) { + if (s_domain == domain && + s_bus == bus && + s_dev == dev && + s_func == func) { + rc = 1; + goto out; + } + } +out: + fclose(f); + return rc; +} + +static int pciback_dev_assign(libxl__gc *gc, unsigned int domain, + unsigned int bus, unsigned int dev, + unsigned int func) +{ + int rc; + + if ( (rc = pciback_dev_has_slot(gc, domain, bus, dev, func)) < 0 ) { + LOGE(ERROR, "Error checking for pciback slot"); + return ERROR_FAIL; + } else if (rc == 0) { + if ( sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER"/new_slot", + domain, bus, dev, func) < 0 ) { + LOGE(ERROR, "Couldn't bind device to pciback!"); + return ERROR_FAIL; + } + } + + if ( sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER"/bind", + domain, bus, dev, func) < 0 ) { + LOGE(ERROR, "Couldn't bind device to pciback!"); + return ERROR_FAIL; + } + return 0; +} + +static int process_make_assignable(libxl__gc *gc, yajl_gen gen, + char *command_name, + const struct libxl__json_object *request, + struct libxl__json_object **response) +{ + struct stat st; + const struct libxl__json_object *json_o; + unsigned int dom, bus, dev, func; + int rc; + bool rebind; + char *spath, *driver_path = NULL; + + json_o = libxl__json_map_get(PCID_MSG_FIELD_SBDF, request, JSON_STRING); + if (!json_o) { + make_error_reply(gc, gen, "No mandatory parameter 'sbdf'", command_name); + return ERROR_FAIL; + } + + if (sscanf(libxl__json_object_get_string(json_o), PCID_SBDF_FMT, + &dom, &bus, &dev, &func) != 4) { + make_error_reply(gc, gen, "Can't parse SBDF", command_name); + return ERROR_FAIL; + } + + json_o = libxl__json_map_get(PCID_MSG_FIELD_REBIND, request, JSON_BOOL); + if (!json_o) { + make_error_reply(gc, gen, "No mandatory parameter 'rebind'", command_name); + return ERROR_FAIL; + } + + rebind = libxl__json_object_get_bool(json_o); + + /* See if the device exists */ + spath = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF, dom, bus, dev, func); + if ( lstat(spath, &st) ) { + make_error_reply(gc, gen, strerror(errno), command_name); + LOGE(ERROR, "Couldn't lstat %s", spath); + return ERROR_FAIL; + } + + /* Check to see if it's already assigned to pciback */ + rc = pciback_dev_is_assigned(gc, dom, bus, dev, func); + if (rc < 0) { + make_error_reply(gc, gen, "Can't check if device is assigned", + command_name); + return ERROR_FAIL; + } + if (rc) { + LOG(WARN, PCI_BDF" already assigned to pciback", dom, bus, dev, func); + goto done; + } + + /* Check to see if there's already a driver that we need to unbind from */ + if (sysfs_dev_unbind(gc, dom, bus, dev, func, &driver_path)) { + LOG(ERROR, "Couldn't unbind "PCI_BDF" from driver", + dom, bus, dev, func); + return ERROR_FAIL; + } + + /* Store driver_path for rebinding back */ + if (rebind) { + if (driver_path) { + pcid_info_xs_write(gc, dom, bus, dev, func, "driver_path", + driver_path); + } else if ( (driver_path = + pcid_info_xs_read(gc, dom, bus, dev, func, + "driver_path")) != NULL ) { + LOG(INFO, PCI_BDF" not bound to a driver, will be rebound to %s", + dom, bus, dev, func, driver_path); + } else { + LOG(WARN, PCI_BDF" not bound to a driver, will not be rebound.", + dom, bus, dev, func); + } + } else { + pcid_info_xs_remove(gc, dom, bus, dev, func, "driver_path"); + } + + if (pciback_dev_assign(gc, dom, bus, dev, func)) { + LOG(ERROR, "Couldn't bind device to pciback!"); + return ERROR_FAIL; + } + +done: + return 0; +} + +static int pciback_dev_unassign(libxl__gc *gc, unsigned int domain, + unsigned int bus, unsigned int dev, + unsigned int func) +{ + /* Remove from pciback */ + if ( sysfs_dev_unbind(gc, domain, bus, dev, func, NULL) < 0 ) { + LOG(ERROR, "Couldn't unbind device!"); + return ERROR_FAIL; + } + + /* Remove slot if necessary */ + if ( pciback_dev_has_slot(gc, domain, bus, dev, func) > 0 ) { + if ( sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER"/remove_slot", + domain, bus, dev, func) < 0 ) { + LOGE(ERROR, "Couldn't remove pciback slot"); + return ERROR_FAIL; + } + } + return 0; +} + +static int process_revert_assignable(libxl__gc *gc, yajl_gen gen, + char *command_name, + const struct libxl__json_object *request, + struct libxl__json_object **response) +{ + const struct libxl__json_object *json_o; + unsigned int dom, bus, dev, func; + int rc; + bool rebind; + char *driver_path = NULL; + + json_o = libxl__json_map_get(PCID_MSG_FIELD_SBDF, request, JSON_STRING); + if (!json_o) { + make_error_reply(gc, gen, "No mandatory parameter 'sbdf'", command_name); + return ERROR_FAIL; + } + + if (sscanf(libxl__json_object_get_string(json_o), PCID_SBDF_FMT, + &dom, &bus, &dev, &func) != 4) { + make_error_reply(gc, gen, "Can't parse SBDF", command_name); + return ERROR_FAIL; + } + + json_o = libxl__json_map_get(PCID_MSG_FIELD_REBIND, request, JSON_BOOL); + if (!json_o) { + make_error_reply(gc, gen, "No mandatory parameter 'rebind'", command_name); + return ERROR_FAIL; + } + + rebind = libxl__json_object_get_bool(json_o); + + /* Unbind from pciback */ + if ( (rc = pciback_dev_is_assigned(gc, dom, bus, dev, func)) < 0 ) { + make_error_reply(gc, gen, "Can't unbind from pciback", command_name); + return ERROR_FAIL; + } else if ( rc ) { + pciback_dev_unassign(gc, dom, bus, dev, func); + } else { + LOG(WARN, "Not bound to pciback"); + } + + /* Rebind if necessary */ + driver_path = pcid_info_xs_read(gc, dom, bus, dev, func, "driver_path"); + + if ( driver_path ) { + if ( rebind ) { + LOG(INFO, "Rebinding to driver at %s", driver_path); + + if ( sysfs_write_bdf(gc, + GCSPRINTF("%s/bind", driver_path), + dom, bus, dev, func) < 0 ) { + LOGE(ERROR, "Couldn't bind device to %s", driver_path); + return -1; + } + + pcid_info_xs_remove(gc, dom, bus, dev, func, "driver_path"); + } + } else { + if ( rebind ) { + LOG(WARN, + "Couldn't find path for original driver; not rebinding"); + } + } + + return 0; +} + static int pcid_handle_request(libxl__gc *gc, yajl_gen gen, const libxl__json_object *request) { @@ -142,6 +532,12 @@ static int pcid_handle_request(libxl__gc *gc, yajl_gen gen, if (strcmp(command_name, PCID_CMD_LIST_ASSIGNABLE) == 0) ret = process_list_assignable(gc, gen, command_name, request, &command_response); + else if (strcmp(command_name, PCID_CMD_MAKE_ASSIGNABLE) == 0) + ret = process_make_assignable(gc, gen, command_name, + request, &command_response); + else if (strcmp(command_name, PCID_CMD_REVERT_ASSIGNABLE) == 0) + ret = process_revert_assignable(gc, gen, command_name, + request, &command_response); else { /* * This is an unsupported command: make a reply and proceed over From patchwork Thu Feb 9 15:56:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmytro Semenets X-Patchwork-Id: 13134830 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.xenproject.org (lists.xenproject.org [192.237.175.120]) (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 5F761C6379F for ; Thu, 9 Feb 2023 15:56:52 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.492676.762359 (Exim 4.92) (envelope-from ) id 1pQ9Ho-0007gM-1j; Thu, 09 Feb 2023 15:56:36 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 492676.762359; Thu, 09 Feb 2023 15:56:36 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pQ9Hn-0007eU-Sm; Thu, 09 Feb 2023 15:56:35 +0000 Received: by outflank-mailman (input) for mailman id 492676; Thu, 09 Feb 2023 15:56:34 +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 1pQ9Hm-0007Ak-AH for xen-devel@lists.xenproject.org; Thu, 09 Feb 2023 15:56:34 +0000 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [2a00:1450:4864:20::536]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 53603fd5-a892-11ed-933c-83870f6b2ba8; Thu, 09 Feb 2023 16:56:33 +0100 (CET) Received: by mail-ed1-x536.google.com with SMTP id l12so2559643edb.0 for ; Thu, 09 Feb 2023 07:56:33 -0800 (PST) Received: from dsemenets-HP-EliteBook-850-G8-Notebook-PC.. ([91.219.254.72]) by smtp.gmail.com with ESMTPSA id v1-20020a170906b00100b0087862f45a29sm1027594ejy.174.2023.02.09.07.56.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Feb 2023 07:56:32 -0800 (PST) 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 Sender: "Xen-devel" X-Inumbo-ID: 53603fd5-a892-11ed-933c-83870f6b2ba8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/WzhJP9vhJfuiyyeXqbJkYqC2dcjt2F4IfvVYqlat9I=; b=RbS+SRI0DAe2NW3ljtd+w56OHWnEQ0kuZADvIx4vdHTld72mZdDC3W5dzvB12y9iv9 AHt/Ave5j/NWiQTXGZHMuuP2n+PlxxySMDWSI+DwUoPyi63O6nofra2XV2MqgveFRaR1 +J84hurhWYxdgnlAfMBEAZ/KwZwVrqj/tpaXko2/oWpNtnQNd607WurF1qxoV3sZbAud WSBCKcpHCUY/gneQj9/z3c+JRxn7Ik8jK+c/iKOZ3fIq2nJCOnocz8YEUEoyElgHREyg HtFESFV952blNkHsUCqJZGs7w3N/mkT/rmw3J340qpam21S93yfHJzZJrmxZfS0jIX6N N/Qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/WzhJP9vhJfuiyyeXqbJkYqC2dcjt2F4IfvVYqlat9I=; b=SfxuPy10RjZs0F/N631heSOiuLIFFRpqNNeabiXyURa3QbeOxA3cSj/vRKiDYENOGh EuzGmYt1KAXrxKbQ1fotP06sLk1nXA2TtUB8gSCnUzHR1VmpTi4yiFzHnsfa2xFt0Z2P EnGzckJkHUmMlCLIOo/zpKAC9C8aWkUh0zY6Kdi0RaLfEXIUTkiQYvMY6p4agzf16l97 4Skv8/tYS0xhwPXdPTIfLWAJE9aA6SwO9vlagTGoTjZWXKIB4coyuhQ8FmGTXL/y8KLl kdm7FxH2Ipp08nE3cKBStbXPbIxMylDLKrMgkm/qLOFRmc1mxGmRYYSxQ9fATco8yl2I 1jIQ== X-Gm-Message-State: AO0yUKW0xtcsRCnjvWGakKo2+0H5oViXGUNSyokF7pRxb+eJkrRAsP7k EQpFeLh5GFasqHdeQdjXe+M+1+nYWC15Vg== X-Google-Smtp-Source: AK7set8X2pu/O+NwWpgWtYA7j/136tsDHLByBf2Zdygp1XeRwKhHiRrK2eba1uzSt8xRbY1PBn4POg== X-Received: by 2002:a05:6402:268b:b0:4a2:2e9e:c2ec with SMTP id w11-20020a056402268b00b004a22e9ec2ecmr14461550edd.4.1675958192956; Thu, 09 Feb 2023 07:56:32 -0800 (PST) From: dmitry.semenets@gmail.com To: xen-devel@lists.xenproject.org Cc: Dmytro Semenets , Wei Liu , Anthony PERARD , Juergen Gross Subject: [RFC PATCH v4 6/9] tools/libs/light: pcid: implement is_device_assigned command Date: Thu, 9 Feb 2023 17:56:00 +0200 Message-Id: <20230209155604.2753219-7-dmitry.semenets@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209155604.2753219-1-dmitry.semenets@gmail.com> References: <20230209155604.2753219-1-dmitry.semenets@gmail.com> MIME-Version: 1.0 From: Dmytro Semenets Signed-off-by: Dmytro Semenets --- tools/include/xen-pcid.h | 19 ++++++++++++++++ tools/libs/light/libxl_pci.c | 43 +++++++++++++++++++---------------- tools/libs/light/libxl_pcid.c | 34 ++++++++++++++++++++++++++- 3 files changed, 75 insertions(+), 21 deletions(-) diff --git a/tools/include/xen-pcid.h b/tools/include/xen-pcid.h index 5e96b8de6e..843066a54c 100644 --- a/tools/include/xen-pcid.h +++ b/tools/include/xen-pcid.h @@ -136,6 +136,25 @@ */ #define PCID_CMD_REVERT_ASSIGNABLE "revert_assignable" +/* + ******************************************************************************* + * Check is device assigned + * + * This command checks device is assigned + * + * Request (see other mandatory fields above): + * - "cmd" field of the request must be set to "is_device_assigned". + * - "sbdf" SBDF of the device in format defined by PCID_SBDF_FMT. + * + * Response (see other mandatory fields above): + * - "resp" field of the response must be set to "is_device_assigned". + * Command specific response data: + * +-------------+--------------+----------------------------------------------+ + * | result | bool | true if device assigned | + * +-------------+--------------+----------------------------------------------+ + */ +#define PCID_CMD_IS_ASSIGNED "is_device_assigned" +#define PCID_MSG_FIELD_RESULT "result" int libxl_pcid_process(libxl_ctx *ctx); diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c index b3dfd95fd3..841b97172d 100644 --- a/tools/libs/light/libxl_pci.c +++ b/tools/libs/light/libxl_pci.c @@ -86,7 +86,9 @@ static int pci_handle_response(libxl__gc *gc, *result = libxl__json_object_alloc(gc, JSON_NULL); else if (strcmp(command_name, PCID_CMD_REVERT_ASSIGNABLE) == 0) *result = libxl__json_object_alloc(gc, JSON_NULL); - + else if (strcmp(command_name, PCID_CMD_IS_ASSIGNED) == 0) + *result = (libxl__json_object *)libxl__json_map_get(PCID_MSG_FIELD_RESULT, + response, JSON_BOOL); return ret; } @@ -753,30 +755,31 @@ bool libxl__is_igd_vga_passthru(libxl__gc *gc, static int pciback_dev_is_assigned(libxl__gc *gc, libxl_device_pci *pci) { - char * spath; + struct vchan_info *vchan; int rc; - struct stat st; + libxl__json_object *args, *result; - if ( access(SYSFS_PCIBACK_DRIVER, F_OK) < 0 ) { - if ( errno == ENOENT ) { - LOG(ERROR, "Looks like pciback driver is not loaded"); - } else { - LOGE(ERROR, "Can't access "SYSFS_PCIBACK_DRIVER); - } - return -1; + vchan = pci_vchan_get_client(gc); + if (!vchan) { + rc = ERROR_NOT_READY; + goto out; } - spath = GCSPRINTF(SYSFS_PCIBACK_DRIVER"/"PCI_BDF, - pci->domain, pci->bus, - pci->dev, pci->func); - rc = lstat(spath, &st); + args = libxl__vchan_start_args(gc); - if( rc == 0 ) - return 1; - if ( rc < 0 && errno == ENOENT ) - return 0; - LOGE(ERROR, "Accessing %s", spath); - return -1; + libxl__vchan_arg_add_string(gc, args, PCID_MSG_FIELD_SBDF, + GCSPRINTF(PCID_SBDF_FMT, pci->domain, + pci->bus, pci->dev, pci->func)); + + result = vchan_send_command(gc, vchan, PCID_CMD_IS_ASSIGNED, args); + if (!result) { + rc = ERROR_FAIL; + } + rc = result->u.b; + pci_vchan_free(gc, vchan); + +out: + return rc; } static int libxl__device_pci_assignable_add(libxl__gc *gc, diff --git a/tools/libs/light/libxl_pcid.c b/tools/libs/light/libxl_pcid.c index 5e1e41feb5..7b6c7fe6a6 100644 --- a/tools/libs/light/libxl_pcid.c +++ b/tools/libs/light/libxl_pcid.c @@ -147,7 +147,7 @@ static int pciback_dev_is_assigned(libxl__gc *gc, unsigned int domain, if (rc < 0 && errno == ENOENT) return 0; LOGE(ERROR, "Accessing %s", spath); - return -1; + return 0; } #define PCID_INFO_PATH "pcid" @@ -335,6 +335,35 @@ static int pciback_dev_assign(libxl__gc *gc, unsigned int domain, return 0; } +static int process_pciback_dev_is_assigned(libxl__gc *gc, yajl_gen gen, + char *command_name, + const struct libxl__json_object *request, + struct libxl__json_object **response) +{ + const struct libxl__json_object *json_o; + unsigned int dom, bus, dev, func; + int rc; + + libxl__yajl_gen_asciiz(gen, PCID_MSG_FIELD_RESULT); + *response = libxl__json_object_alloc(gc, JSON_BOOL); + json_o = libxl__json_map_get(PCID_MSG_FIELD_SBDF, request, JSON_STRING); + if (!json_o) { + make_error_reply(gc, gen, "No mandatory parameter 'sbdf'", command_name); + return ERROR_FAIL; + } + + if (sscanf(libxl__json_object_get_string(json_o), PCID_SBDF_FMT, + &dom, &bus, &dev, &func) != 4) { + make_error_reply(gc, gen, "Can't parse SBDF", command_name); + return ERROR_FAIL; + } + rc = pciback_dev_is_assigned(gc, dom, bus, dev, func); + if (rc < 0) + return ERROR_FAIL; + (*response)->u.b = rc; + return 0; +} + static int process_make_assignable(libxl__gc *gc, yajl_gen gen, char *command_name, const struct libxl__json_object *request, @@ -538,6 +567,9 @@ static int pcid_handle_request(libxl__gc *gc, yajl_gen gen, else if (strcmp(command_name, PCID_CMD_REVERT_ASSIGNABLE) == 0) ret = process_revert_assignable(gc, gen, command_name, request, &command_response); + else if (strcmp(command_name, PCID_CMD_IS_ASSIGNED) == 0) + ret = process_pciback_dev_is_assigned(gc, gen, command_name, + request, &command_response); else { /* * This is an unsupported command: make a reply and proceed over From patchwork Thu Feb 9 15:56:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmytro Semenets X-Patchwork-Id: 13134831 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.xenproject.org (lists.xenproject.org [192.237.175.120]) (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 64D1AC64EC4 for ; Thu, 9 Feb 2023 15:56:52 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.492677.762368 (Exim 4.92) (envelope-from ) id 1pQ9Ho-0007qk-NV; Thu, 09 Feb 2023 15:56:36 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 492677.762368; Thu, 09 Feb 2023 15:56:36 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pQ9Ho-0007nh-El; Thu, 09 Feb 2023 15:56:36 +0000 Received: by outflank-mailman (input) for mailman id 492677; Thu, 09 Feb 2023 15:56:35 +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 1pQ9Hn-0007Ak-Cl for xen-devel@lists.xenproject.org; Thu, 09 Feb 2023 15:56:35 +0000 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [2a00:1450:4864:20::635]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 53ffdf13-a892-11ed-933c-83870f6b2ba8; Thu, 09 Feb 2023 16:56:34 +0100 (CET) Received: by mail-ej1-x635.google.com with SMTP id lu11so7732123ejb.3 for ; Thu, 09 Feb 2023 07:56:34 -0800 (PST) Received: from dsemenets-HP-EliteBook-850-G8-Notebook-PC.. ([91.219.254.72]) by smtp.gmail.com with ESMTPSA id v1-20020a170906b00100b0087862f45a29sm1027594ejy.174.2023.02.09.07.56.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Feb 2023 07:56:33 -0800 (PST) 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 Sender: "Xen-devel" X-Inumbo-ID: 53ffdf13-a892-11ed-933c-83870f6b2ba8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hqOdci0RnBqfM9zuF9NcrL5pTTcWWQ4ya+aJqtMi5ro=; b=KQTeOkBryB2Q8oZq5V1y4TlLeWD66B8FECePbXBBby9JLo4VuAk856HunjSpdJaKMU lZsB1d0mk7aghoHYPjZ4vDJ3hfInS2J5/I2e7NYut68prIHrSAsAOvGrwnP6PDJWGKTD dWfH5j03UBrsFxbP8xguXINWV05au4EpUFCMY7HuQ5yLOVhdNiEU6cQdeLmmC/SypO7D iPXF01qMycAZgo/4qgLjIYy6gRcfC86ekJulvV7UpJCi1z8VW2djKpoXL/5Ya7UEy4Ge TPgNYatHxQp9mUuZtiA5qDW4cjVzHmy+QOSPTXJ+j6xzscLt3J0ygTsvD/EnFFj24afN YkTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hqOdci0RnBqfM9zuF9NcrL5pTTcWWQ4ya+aJqtMi5ro=; b=NoWbTutExY9HiBS6LWNaQf8iW/06QCmB+TUdfiQ4fbOGSRaU9Qbq6zmisGUzcU4Fie XywC4tvyk8e2u94MjfcYRlrFIHz82q/mxyYXEqg2FegLK2/w2mbQ5qTtq16v3rd2hcgB gQVsS4aiKBIjonk9G+SFxzDoBpBY5kWDkrV2rAq5RztwcOU2ilj+nmR8TuSjGZMy0W7E 6IXLeSCwoZwJoIJ5UIBaidiyThHCi6/e1DGER+dsL7GnPsOGIkjv9a7KSIKE5w2IzqVX xlpBDzXcbps1sZu30Maf6dCI3EiFsSkl83lUi5+duUmWGa1+9moYsC/2dZyYEkAaAiEj jngA== X-Gm-Message-State: AO0yUKWZPVnht7F9wonUYI8eC5ePmTKVAiDJrMFQlGOHwrCdFiaC2KQD w2fjtJCMSygy3ilLYReMJdAuCCBvORGnbg== X-Google-Smtp-Source: AK7set8b/9mmAz5+IWSOO/j4AOTz5RYR51wseZ5L3zVSGxCuVqTqhs2WxC4zTSOAJxwdKL4In7fRcg== X-Received: by 2002:a17:906:1011:b0:8af:11b5:fabd with SMTP id 17-20020a170906101100b008af11b5fabdmr5210562ejm.5.1675958194017; Thu, 09 Feb 2023 07:56:34 -0800 (PST) From: dmitry.semenets@gmail.com To: xen-devel@lists.xenproject.org Cc: Dmytro Semenets , Wei Liu , Anthony PERARD , Juergen Gross Subject: [RFC PATCH v4 7/9] tools/libs/light: pcid: implement reset_device command Date: Thu, 9 Feb 2023 17:56:01 +0200 Message-Id: <20230209155604.2753219-8-dmitry.semenets@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209155604.2753219-1-dmitry.semenets@gmail.com> References: <20230209155604.2753219-1-dmitry.semenets@gmail.com> MIME-Version: 1.0 From: Dmytro Semenets Signed-off-by: Dmytro Semenets --- tools/include/xen-pcid.h | 15 ++++++++ tools/libs/light/libxl_pci.c | 52 +++++++++++---------------- tools/libs/light/libxl_pcid.c | 66 +++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 31 deletions(-) diff --git a/tools/include/xen-pcid.h b/tools/include/xen-pcid.h index 843066a54c..65be441250 100644 --- a/tools/include/xen-pcid.h +++ b/tools/include/xen-pcid.h @@ -156,6 +156,21 @@ #define PCID_CMD_IS_ASSIGNED "is_device_assigned" #define PCID_MSG_FIELD_RESULT "result" +/* + ******************************************************************************* + * Reset PCI device + * + * This command resets PCI device + * + * Request (see other mandatory fields above): + * - "cmd" field of the request must be set to "reset_device". + * - "sbdf" SBDF of the device in format defined by PCID_SBDF_FMT. + * + * Response (see other mandatory fields above): + * - "resp" field of the response must be set to "reset_device". + */ +#define PCID_CMD_RESET_DEVICE "reset_device" + int libxl_pcid_process(libxl_ctx *ctx); #endif /* XEN_PCID_H */ diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c index 841b97172d..e920a1fa4b 100644 --- a/tools/libs/light/libxl_pci.c +++ b/tools/libs/light/libxl_pci.c @@ -89,6 +89,8 @@ static int pci_handle_response(libxl__gc *gc, else if (strcmp(command_name, PCID_CMD_IS_ASSIGNED) == 0) *result = (libxl__json_object *)libxl__json_map_get(PCID_MSG_FIELD_RESULT, response, JSON_BOOL); + else if (strcmp(command_name, PCID_CMD_RESET_DEVICE) == 0) + *result = libxl__json_object_alloc(gc, JSON_NULL); return ret; } @@ -1518,38 +1520,26 @@ out: static int libxl__device_pci_reset(libxl__gc *gc, unsigned int domain, unsigned int bus, unsigned int dev, unsigned int func) { - char *reset; - int fd, rc; - - reset = GCSPRINTF("%s/do_flr", SYSFS_PCIBACK_DRIVER); - fd = open(reset, O_WRONLY); - if (fd >= 0) { - char *buf = GCSPRINTF(PCI_BDF, domain, bus, dev, func); - rc = write(fd, buf, strlen(buf)); - if (rc < 0) - LOGD(ERROR, domain, "write to %s returned %d", reset, rc); - close(fd); - return rc < 0 ? rc : 0; - } - if (errno != ENOENT) - LOGED(ERROR, domain, "Failed to access pciback path %s", reset); - reset = GCSPRINTF("%s/"PCI_BDF"/reset", SYSFS_PCI_DEV, domain, bus, dev, func); - fd = open(reset, O_WRONLY); - if (fd >= 0) { - rc = write(fd, "1", 1); - if (rc < 0) - LOGED(ERROR, domain, "write to %s returned %d", reset, rc); - close(fd); - return rc < 0 ? rc : 0; - } - if (errno == ENOENT) { - LOGD(ERROR, domain, - "The kernel doesn't support reset from sysfs for PCI device "PCI_BDF, - domain, bus, dev, func); - } else { - LOGED(ERROR, domain, "Failed to access reset path %s", reset); + struct vchan_info *vchan; + int rc = 0; + libxl__json_object *args, *result; + + vchan = pci_vchan_get_client(gc); + if (!vchan) { + rc = ERROR_NOT_READY; + goto out; } - return -1; + args = libxl__vchan_start_args(gc); + + libxl__vchan_arg_add_string(gc, args, PCID_MSG_FIELD_SBDF, + GCSPRINTF(PCID_SBDF_FMT, domain, bus, dev, func)); + result = vchan_send_command(gc, vchan, PCID_CMD_RESET_DEVICE, args); + if (!result) + rc = ERROR_FAIL; + pci_vchan_free(gc, vchan); + + out: + return rc; } int libxl__device_pci_setdefault(libxl__gc *gc, uint32_t domid, diff --git a/tools/libs/light/libxl_pcid.c b/tools/libs/light/libxl_pcid.c index 7b6c7fe6a6..05c0b69324 100644 --- a/tools/libs/light/libxl_pcid.c +++ b/tools/libs/light/libxl_pcid.c @@ -364,6 +364,69 @@ static int process_pciback_dev_is_assigned(libxl__gc *gc, yajl_gen gen, return 0; } +static int device_pci_reset(libxl__gc *gc, unsigned int domain, unsigned int bus, + unsigned int dev, unsigned int func) +{ + char *reset; + int fd, rc; + + reset = GCSPRINTF("%s/do_flr", SYSFS_PCIBACK_DRIVER); + fd = open(reset, O_WRONLY); + if (fd >= 0) { + char *buf = GCSPRINTF(PCI_BDF, domain, bus, dev, func); + rc = write(fd, buf, strlen(buf)); + if (rc < 0) + LOGD(ERROR, domain, "write to %s returned %d", reset, rc); + close(fd); + return rc < 0 ? rc : 0; + } + if (errno != ENOENT) + LOGED(ERROR, domain, "Failed to access pciback path %s", reset); + reset = GCSPRINTF("%s/"PCI_BDF"/reset", SYSFS_PCI_DEV, domain, bus, dev, func); + fd = open(reset, O_WRONLY); + if (fd >= 0) { + rc = write(fd, "1", 1); + if (rc < 0) + LOGED(ERROR, domain, "write to %s returned %d", reset, rc); + close(fd); + return rc < 0 ? rc : 0; + } + if (errno == ENOENT) { + LOGD(ERROR, domain, + "The kernel doesn't support reset from sysfs for PCI device "PCI_BDF, + domain, bus, dev, func); + } else { + LOGED(ERROR, domain, "Failed to access reset path %s", reset); + } + return -1; +} + +static int process_device_pci_reset(libxl__gc *gc, yajl_gen gen, + char *command_name, + const struct libxl__json_object *request, + struct libxl__json_object **response) +{ + const struct libxl__json_object *json_o; + unsigned int dom, bus, dev, func; + int rc; + + json_o = libxl__json_map_get(PCID_MSG_FIELD_SBDF, request, JSON_STRING); + if (!json_o) { + make_error_reply(gc, gen, "No mandatory parameter 'sbdf'", command_name); + return ERROR_FAIL; + } + + if (sscanf(libxl__json_object_get_string(json_o), PCID_SBDF_FMT, + &dom, &bus, &dev, &func) != 4) { + make_error_reply(gc, gen, "Can't parse SBDF", command_name); + return ERROR_FAIL; + } + rc = device_pci_reset(gc, dom, bus, dev, func); + if (rc < 0) + return ERROR_FAIL; + return rc; +} + static int process_make_assignable(libxl__gc *gc, yajl_gen gen, char *command_name, const struct libxl__json_object *request, @@ -570,6 +633,9 @@ static int pcid_handle_request(libxl__gc *gc, yajl_gen gen, else if (strcmp(command_name, PCID_CMD_IS_ASSIGNED) == 0) ret = process_pciback_dev_is_assigned(gc, gen, command_name, request, &command_response); + else if (strcmp(command_name, PCID_CMD_RESET_DEVICE) == 0) + ret = process_device_pci_reset(gc, gen, command_name, + request, &command_response); else { /* * This is an unsupported command: make a reply and proceed over From patchwork Thu Feb 9 15:56:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmytro Semenets X-Patchwork-Id: 13134835 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.xenproject.org (lists.xenproject.org [192.237.175.120]) (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 154E7C6379F for ; Thu, 9 Feb 2023 15:56:57 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.492679.762399 (Exim 4.92) (envelope-from ) id 1pQ9Hr-0000Ku-K2; Thu, 09 Feb 2023 15:56:39 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 492679.762399; Thu, 09 Feb 2023 15:56:39 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pQ9Hr-0000Io-BB; Thu, 09 Feb 2023 15:56:39 +0000 Received: by outflank-mailman (input) for mailman id 492679; Thu, 09 Feb 2023 15:56:37 +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 1pQ9Ho-0007Ak-Pn for xen-devel@lists.xenproject.org; Thu, 09 Feb 2023 15:56:37 +0000 Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [2a00:1450:4864:20::629]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 549b6a4a-a892-11ed-933c-83870f6b2ba8; Thu, 09 Feb 2023 16:56:35 +0100 (CET) Received: by mail-ej1-x629.google.com with SMTP id sa10so7667153ejc.9 for ; Thu, 09 Feb 2023 07:56:35 -0800 (PST) Received: from dsemenets-HP-EliteBook-850-G8-Notebook-PC.. ([91.219.254.72]) by smtp.gmail.com with ESMTPSA id v1-20020a170906b00100b0087862f45a29sm1027594ejy.174.2023.02.09.07.56.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Feb 2023 07:56:34 -0800 (PST) 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 Sender: "Xen-devel" X-Inumbo-ID: 549b6a4a-a892-11ed-933c-83870f6b2ba8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Dce94Jrl1fONboV6egU+yvi+P6trFx/LtuUcWDWz0QA=; b=IhnidZZp4ea6CBm47aSbqq7R1WCTIMgi4psDVc0OE6qhmbLyVc1pyEFY6HNMW1FQ4K /sQtr+cVkFwXmiVCnpkYpJakN+5GALP2GKhH4Gf4Jbhf8Mj3W0QZz8WJ8bu8if3rId1o noExBk+hMw4q/J0cmRwflAEMSYGufg9qIdlSribwOM7WAbgKKlJFBNNRrQiOb+1yJJva rqgPrhJrvN8oxVEWgGqrF5woObUbgypZS7HZqqydgmh5zAC0Th+sbgZ3JZ5MNoRGNsca UqKnIwIkFlyZ1sKzBnxBsdBUDlZ7re6bNoXO+Qavf351nZqmBIDh4SWMfNJ+ixtON9KA 6luA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Dce94Jrl1fONboV6egU+yvi+P6trFx/LtuUcWDWz0QA=; b=KsCKsphsg/UYSIsQbQdzbqlo6ZiX1QWov7XALflJDaM1ySNwE8UD1gi1IZEN0bOGoR S7XhKCZd3Fi3AOwx0PN2AnLRkqJvM/W7DCR/MSwLTCz7RDR4pdnP+qp965orGbqq00aI Jy3RdeXyV9fmV5p11hsep7TBWebSQXLXSNJJK9XplWCY7hI6PoldkgOPqjpLZalF8ENj IsZNjNc4z/gZaPkGmDqETZ367v7y/2S98gouS5yQych7hnC5KPeiQh0VVZmx5uKqL4Zi xgqbJBzh6RQGPfDUT1W7ycB4ukFD289ZVui/NzpXnb1C4qeCT3+GyQJOVurADSsk713d dcAg== X-Gm-Message-State: AO0yUKUsR672psjKYYV9sjJ5FE6N37ihgGbvbAk1jBPx+ZmSwXBLvVZb 1cb+/rvEwjjJVe6ygUwF/TrlihM/DFYtNg== X-Google-Smtp-Source: AK7set8pRZCT1H9vWbqv9sxCrORaJZd+fnWVtZoHfl2TftEMSIR+RGUkeXqiN7vejRoVCOr4Fbjgdw== X-Received: by 2002:a17:906:2085:b0:8aa:a9df:b7f0 with SMTP id 5-20020a170906208500b008aaa9dfb7f0mr11655733ejq.7.1675958195071; Thu, 09 Feb 2023 07:56:35 -0800 (PST) From: dmitry.semenets@gmail.com To: xen-devel@lists.xenproject.org Cc: Dmytro Semenets , Wei Liu , Anthony PERARD , Juergen Gross Subject: [RFC PATCH v4 8/9] tools/libs/light: pcid: implement resource_list command Date: Thu, 9 Feb 2023 17:56:02 +0200 Message-Id: <20230209155604.2753219-9-dmitry.semenets@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209155604.2753219-1-dmitry.semenets@gmail.com> References: <20230209155604.2753219-1-dmitry.semenets@gmail.com> MIME-Version: 1.0 From: Dmytro Semenets Signed-off-by: Dmytro Semenets --- tools/include/xen-pcid.h | 26 ++++++++ tools/libs/light/libxl_pci.c | 63 ++++++++++--------- tools/libs/light/libxl_pcid.c | 110 +++++++++++++++++++++++++++++++++ tools/libs/light/libxl_vchan.c | 8 +++ tools/libs/light/libxl_vchan.h | 4 +- 5 files changed, 180 insertions(+), 31 deletions(-) diff --git a/tools/include/xen-pcid.h b/tools/include/xen-pcid.h index 65be441250..ad2dec80df 100644 --- a/tools/include/xen-pcid.h +++ b/tools/include/xen-pcid.h @@ -156,6 +156,32 @@ #define PCID_CMD_IS_ASSIGNED "is_device_assigned" #define PCID_MSG_FIELD_RESULT "result" +/* + ******************************************************************************* + * Get device resources + * + * This command returns resource list of device + * + * Request (see other mandatory fields above): + * - "cmd" field of the request must be set to "resource_list". + * - "sbdf" SBDF of the device in format defined by PCID_SBDF_FMT. + * + * Response (see other mandatory fields above): + * - "resp" field of the response must be set to "resource_list". + * Command specific response data: + * +-------------+--------------+----------------------------------------------+ + * | resources | map | key 'iomem' - list of memory regions | + * | | | key 'irqs' - list of irqs | + * +-------------+--------------+----------------------------------------------+ + */ +#define PCID_CMD_RESOURCE_LIST "resource_list" +/* Arguments */ +#define PCID_MSG_FIELD_DOMID "domid" +/* Result */ +#define PCID_MSG_FIELD_RESOURCES "resources" +#define PCID_RESULT_KEY_IOMEM "iomem" +#define PCID_RESULT_KEY_IRQS "irqs" + /* ******************************************************************************* * Reset PCI device diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c index e920a1fa4b..955635125f 100644 --- a/tools/libs/light/libxl_pci.c +++ b/tools/libs/light/libxl_pci.c @@ -81,16 +81,19 @@ static int pci_handle_response(libxl__gc *gc, LOG(DEBUG, "command: %s", command_name); if (strcmp(command_name, PCID_CMD_LIST_ASSIGNABLE) == 0) - ret = process_list_assignable(gc, response, result); + ret = process_list_assignable(gc, response, result); else if (strcmp(command_name, PCID_CMD_MAKE_ASSIGNABLE) == 0) *result = libxl__json_object_alloc(gc, JSON_NULL); else if (strcmp(command_name, PCID_CMD_REVERT_ASSIGNABLE) == 0) *result = libxl__json_object_alloc(gc, JSON_NULL); else if (strcmp(command_name, PCID_CMD_IS_ASSIGNED) == 0) *result = (libxl__json_object *)libxl__json_map_get(PCID_MSG_FIELD_RESULT, - response, JSON_BOOL); + response, JSON_BOOL); else if (strcmp(command_name, PCID_CMD_RESET_DEVICE) == 0) *result = libxl__json_object_alloc(gc, JSON_NULL); + else if (strcmp(command_name, PCID_CMD_RESOURCE_LIST) == 0) + *result = (libxl__json_object *)libxl__json_map_get(PCID_MSG_FIELD_RESOURCES, + response, JSON_MAP); return ret; } @@ -1388,14 +1391,21 @@ static void pci_add_dm_done(libxl__egc *egc, STATE_AO_GC(pas->aodev->ao); libxl_ctx *ctx = libxl__gc_owner(gc); libxl_domid domid = pas->pci_domid; - char *sysfs_path; - FILE *f; unsigned long long start, end, flags, size; int irq, i; int r; uint32_t flag = XEN_DOMCTL_DEV_RDM_RELAXED; uint32_t domainid = domid; bool isstubdom = libxl_is_stubdom(ctx, domid, &domainid); + struct vchan_info *vchan; + libxl__json_object *result; + libxl__json_object *args; + const libxl__json_object *value; + libxl__json_object *res_obj; + + vchan = pci_vchan_get_client(gc); + if (!vchan) + goto out; /* Convenience aliases */ bool starting = pas->starting; @@ -1404,25 +1414,27 @@ static void pci_add_dm_done(libxl__egc *egc, libxl__ev_qmp_dispose(gc, &pas->qmp); - if (rc) goto out; + args = libxl__vchan_start_args(gc); + libxl__vchan_arg_add_string(gc, args, PCID_MSG_FIELD_SBDF, + GCSPRINTF(PCID_SBDF_FMT, pci->domain, + pci->bus, pci->dev, pci->func)); + libxl__vchan_arg_add_integer(gc, args, PCID_MSG_FIELD_DOMID, domid); + + result = vchan_send_command(gc, vchan, PCID_CMD_RESOURCE_LIST, args); + if (!result) + goto vchan_free; + value = libxl__json_map_get(PCID_RESULT_KEY_IOMEM, result, JSON_ARRAY); /* stubdomain is always running by now, even at create time */ if (isstubdom) starting = false; - - sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/resource", pci->domain, - pci->bus, pci->dev, pci->func); - f = fopen(sysfs_path, "r"); start = end = flags = size = 0; irq = 0; - - if (f == NULL) { - LOGED(ERROR, domainid, "Couldn't open %s", sysfs_path); - rc = ERROR_FAIL; - goto out; - } for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) { - if (fscanf(f, "0x%llx 0x%llx 0x%llx\n", &start, &end, &flags) != 3) + if ((res_obj = libxl__json_array_get(value, i)) == NULL) + continue; + const char *iomem_str = libxl__json_object_get_string(res_obj); + if (sscanf(iomem_str, "0x%llx 0x%llx 0x%llx\n", &start, &end, &flags) != 3) continue; size = end - start + 1; if (start) { @@ -1432,7 +1444,6 @@ static void pci_add_dm_done(libxl__egc *egc, LOGED(ERROR, domainid, "xc_domain_ioport_permission 0x%llx/0x%llx (error %d)", start, size, r); - fclose(f); rc = ERROR_FAIL; goto out; } @@ -1443,29 +1454,21 @@ static void pci_add_dm_done(libxl__egc *egc, LOGED(ERROR, domainid, "xc_domain_iomem_permission 0x%llx/0x%llx (error %d)", start, size, r); - fclose(f); rc = ERROR_FAIL; goto out; } } } } - fclose(f); if (!pci_supp_legacy_irq()) goto out_no_irq; - sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domain, - pci->bus, pci->dev, pci->func); - f = fopen(sysfs_path, "r"); - if (f == NULL) { - LOGED(ERROR, domainid, "Couldn't open %s", sysfs_path); - goto out_no_irq; - } - if ((fscanf(f, "%u", &irq) == 1) && irq) { + value = libxl__json_map_get(PCID_RESULT_KEY_IRQS, result, JSON_ARRAY); + if ((res_obj = libxl__json_array_get(value, i)) && + (irq = libxl__json_object_get_integer(res_obj))) { r = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq); if (r < 0) { LOGED(ERROR, domainid, "xc_physdev_map_pirq irq=%d (error=%d)", irq, r); - fclose(f); rc = ERROR_FAIL; goto out; } @@ -1473,12 +1476,10 @@ static void pci_add_dm_done(libxl__egc *egc, if (r < 0) { LOGED(ERROR, domainid, "xc_domain_irq_permission irq=%d (error=%d)", irq, r); - fclose(f); rc = ERROR_FAIL; goto out; } } - fclose(f); /* Don't restrict writes to the PCI config space from this VM */ if (pci->permissive) { @@ -1511,6 +1512,8 @@ out_no_irq: rc = libxl__device_pci_add_xenstore(gc, domid, pci, starting); else rc = 0; +vchan_free: + pci_vchan_free(gc, vchan); out: libxl__ev_time_deregister(gc, &pas->timeout); libxl__ev_time_deregister(gc, &pas->timeout_retries); diff --git a/tools/libs/light/libxl_pcid.c b/tools/libs/light/libxl_pcid.c index 05c0b69324..0befb1999d 100644 --- a/tools/libs/light/libxl_pcid.c +++ b/tools/libs/light/libxl_pcid.c @@ -40,6 +40,10 @@ #define PCI_BDF "%04x:%02x:%02x.%01x" +static int sysfs_write_bdf(libxl__gc *gc, const char * sysfs_path, + unsigned int domain, unsigned int bus, + unsigned int dev, unsigned int func); + struct vchan_client { XEN_LIST_ENTRY(struct vchan_client) list; @@ -121,6 +125,109 @@ static int process_list_assignable(libxl__gc *gc, yajl_gen gen, return 0; } +static bool pci_supp_legacy_irq(void) +{ +#ifdef CONFIG_PCI_SUPP_LEGACY_IRQ + return true; +#else + return false; +#endif +} + +static int process_list_resources(libxl__gc *gc, yajl_gen gen, + char *command_name, + const struct libxl__json_object *request, + struct libxl__json_object **response) +{ + struct libxl__json_object *iomem = + libxl__json_object_alloc(gc, JSON_ARRAY); + struct libxl__json_object *irqs = + libxl__json_object_alloc(gc, JSON_ARRAY); + const struct libxl__json_object *json_sdbf; + const struct libxl__json_object *json_domid; + unsigned int dom, bus, dev, func; + libxl_domid domainid; + char *sysfs_path; + FILE *f; + unsigned long long start, end, flags; + int irq, i; + int rc = 0; + libxl__json_map_node *map_node = NULL; + + json_sdbf = libxl__json_map_get(PCID_MSG_FIELD_SBDF, request, JSON_STRING); + if (!json_sdbf) { + make_error_reply(gc, gen, "No mandatory parameter 'sbdf'", command_name); + return ERROR_FAIL; + } + if (sscanf(libxl__json_object_get_string(json_sdbf), PCID_SBDF_FMT, + &dom, &bus, &dev, &func) != 4) { + make_error_reply(gc, gen, "Can't parse SBDF", command_name); + return ERROR_FAIL; + } + + json_domid = libxl__json_map_get(PCID_MSG_FIELD_DOMID, request, JSON_INTEGER); + if (!json_domid) { + make_error_reply(gc, gen, "No mandatory parameter 'domid'", command_name); + return ERROR_FAIL; + } + domainid = libxl__json_object_get_integer(json_domid); + + libxl__yajl_gen_asciiz(gen, PCID_MSG_FIELD_RESOURCES); + *response = libxl__json_object_alloc(gc, JSON_MAP); + + sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/resource", dom, bus, dev, func); + f = fopen(sysfs_path, "r"); + start = 0; + irq = 0; + + if (f == NULL) { + LOGED(ERROR, domainid, "Couldn't open %s", sysfs_path); + rc = ERROR_FAIL; + goto out; + } + for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) { + if (fscanf(f, "0x%llx 0x%llx 0x%llx\n", &start, &end, &flags) != 3) + continue; + if (start) { + struct libxl__json_object *node = + libxl__json_object_alloc(gc, JSON_STRING); + + node->u.string = GCSPRINTF("0x%llx 0x%llx 0x%llx", start, end, flags); + flexarray_append(iomem->u.array, node); + } + } + fclose(f); + if (!pci_supp_legacy_irq()) + goto out_no_irq; + sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", dom, bus, dev, func); + f = fopen(sysfs_path, "r"); + if (f == NULL) { + LOGED(ERROR, domainid, "Couldn't open %s", sysfs_path); + goto out_no_irq; + } + if ((fscanf(f, "%u", &irq) == 1) && irq) { + struct libxl__json_object *node = + libxl__json_object_alloc(gc, JSON_INTEGER); + + node->u.i = irq; + flexarray_append(irqs->u.array, node); + } + fclose(f); + + GCNEW(map_node); + map_node->map_key = libxl__strdup(gc, PCID_RESULT_KEY_IRQS); + map_node->obj = irqs; + flexarray_append((*response)->u.map, map_node); +out_no_irq: + GCNEW(map_node); + map_node->map_key = libxl__strdup(gc, PCID_RESULT_KEY_IOMEM); + map_node->obj = iomem; + flexarray_append((*response)->u.map, map_node); + rc = 0; +out: + return rc; +} + static int pciback_dev_is_assigned(libxl__gc *gc, unsigned int domain, unsigned int bus, unsigned int dev, unsigned int func) @@ -636,6 +743,9 @@ static int pcid_handle_request(libxl__gc *gc, yajl_gen gen, else if (strcmp(command_name, PCID_CMD_RESET_DEVICE) == 0) ret = process_device_pci_reset(gc, gen, command_name, request, &command_response); + else if (strcmp(command_name, PCID_CMD_RESOURCE_LIST) == 0) + ret = process_list_resources(gc, gen, command_name, + request, &command_response); else { /* * This is an unsupported command: make a reply and proceed over diff --git a/tools/libs/light/libxl_vchan.c b/tools/libs/light/libxl_vchan.c index 7611816f52..a1beda9e1b 100644 --- a/tools/libs/light/libxl_vchan.c +++ b/tools/libs/light/libxl_vchan.c @@ -99,6 +99,14 @@ void libxl__vchan_arg_add_bool(libxl__gc *gc, libxl__json_object *args, obj->u.b = val; } +void libxl__vchan_arg_add_integer(libxl__gc *gc, libxl__json_object *args, + char *key, int val) +{ + libxl__json_object *obj = libxl__vchan_arg_new(gc, JSON_INTEGER, args, key); + + obj->u.i = val; +} + static void reset_yajl_generator(struct vchan_state *state) { yajl_gen_clear(state->gen); diff --git a/tools/libs/light/libxl_vchan.h b/tools/libs/light/libxl_vchan.h index 0968875298..07f0db4e93 100644 --- a/tools/libs/light/libxl_vchan.h +++ b/tools/libs/light/libxl_vchan.h @@ -58,7 +58,9 @@ static inline libxl__json_object *libxl__vchan_start_args(libxl__gc *gc) void libxl__vchan_arg_add_string(libxl__gc *gc, libxl__json_object *args, char *key, char *val); void libxl__vchan_arg_add_bool(libxl__gc *gc, libxl__json_object *args, - char *key, bool val); + char *key, bool val); +void libxl__vchan_arg_add_integer(libxl__gc *gc, libxl__json_object *args, + char *key, int val); libxl__json_object *vchan_send_command(libxl__gc *gc, struct vchan_info *vchan, char *cmd, libxl__json_object *args); From patchwork Thu Feb 9 15:56:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmytro Semenets X-Patchwork-Id: 13134834 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.xenproject.org (lists.xenproject.org [192.237.175.120]) (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 C50EBC636D6 for ; Thu, 9 Feb 2023 15:56:56 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.492680.762406 (Exim 4.92) (envelope-from ) id 1pQ9Hs-0000SI-EL; Thu, 09 Feb 2023 15:56:40 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 492680.762406; Thu, 09 Feb 2023 15:56:40 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pQ9Hs-0000R2-07; Thu, 09 Feb 2023 15:56:40 +0000 Received: by outflank-mailman (input) for mailman id 492680; Thu, 09 Feb 2023 15:56:38 +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 1pQ9Hq-0006cl-Gy for xen-devel@lists.xenproject.org; Thu, 09 Feb 2023 15:56:38 +0000 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [2a00:1450:4864:20::62b]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 553d0ea0-a892-11ed-93b5-47a8fe42b414; Thu, 09 Feb 2023 16:56:36 +0100 (CET) Received: by mail-ej1-x62b.google.com with SMTP id gr7so7725460ejb.5 for ; Thu, 09 Feb 2023 07:56:36 -0800 (PST) Received: from dsemenets-HP-EliteBook-850-G8-Notebook-PC.. ([91.219.254.72]) by smtp.gmail.com with ESMTPSA id v1-20020a170906b00100b0087862f45a29sm1027594ejy.174.2023.02.09.07.56.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Feb 2023 07:56:35 -0800 (PST) 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 Sender: "Xen-devel" X-Inumbo-ID: 553d0ea0-a892-11ed-93b5-47a8fe42b414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0HDRmSfELdHFsxew+hy0RMD843CMREA2B14by0pRDE0=; b=eYe8PdysANwGLi6ir/z7GNejeEdFb/yKqwstsPaWymqLfX8vtQr7IqGQgI+gOfY3LH VFCyJ076kspR56gqmhLKKIexWkAtpnhoAsFtVTMLLaSjNDSMMmlbVpFOoZEqPP7dDTwn n0hq+80Ud/gDYmdBAbtJmjl9OMJoMOGuX5z8l5UQcn1cZ7QmuJQt1wTY+MkEn5Zmfce7 ScyGnS4TCH8ZBXIC/R/AUI3WBf5Kd4k8dZfpPHMQg77sf84sHq7tsYReNfVibj+oWI2r 4mgN8ftv+I0Fq7kJvv+oWrDTOxiWRKDgAvUVS9/SXzN7fz56PHvagVejcYsWTzpjuNpj ig2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0HDRmSfELdHFsxew+hy0RMD843CMREA2B14by0pRDE0=; b=ngxWWP4/ok8nVlqCjcUPrlwN7/Ncnc2BTxCi8hnT0/QaonyDToM41XWe1D8sPEeYG4 6OhT+QlAptCSS6RW7OQYuvafdIuVc3nqOr/MbCvPdLy/GdcDP5TknxGfHID8gwg3/ZnF uQmvfgPvx3qkg2XUcM3Y1j465mj/YOq0bw6a71tFB8yYP0uhDqYANPUsoGvpITEFVaNG cMZTXQvSO5knLI7VBtcTkNNINLLmJRzDFZf9TY2vTQ861b1R8WHs6yab8N1RHLFFyTpt kwS+FQgYwIMpBE/Do/gS3mrWc2160rYQmJQMYBei2MhYsfY8xEedX9yPXS71L/+qCFpU T7QA== X-Gm-Message-State: AO0yUKWU6z9yQnGVe5Ae6fhxFj/ZN8ahHMP917dzP8uphL0frQJVbGVN roW+zNgv83X6g8EiljTa7og6n6Z02KwpTQ== X-Google-Smtp-Source: AK7set8PttQf/A23scnNgYDmyUMC5SpM1LhW3fDa0espq2v56go0uO1k4JI44QLi/+dq2VClCOZYPA== X-Received: by 2002:a17:906:74ca:b0:8a9:e5c2:e3b8 with SMTP id z10-20020a17090674ca00b008a9e5c2e3b8mr10166753ejl.2.1675958196068; Thu, 09 Feb 2023 07:56:36 -0800 (PST) From: dmitry.semenets@gmail.com To: xen-devel@lists.xenproject.org Cc: Dmytro Semenets , Wei Liu , Anthony PERARD , Juergen Gross Subject: [RFC PATCH v4 9/9] tools/libs/light: pcid: implement write_bdf command Date: Thu, 9 Feb 2023 17:56:03 +0200 Message-Id: <20230209155604.2753219-10-dmitry.semenets@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209155604.2753219-1-dmitry.semenets@gmail.com> References: <20230209155604.2753219-1-dmitry.semenets@gmail.com> MIME-Version: 1.0 From: Dmytro Semenets Signed-off-by: Dmytro Semenets --- tools/include/xen-pcid.h | 17 +++++++++ tools/libs/light/libxl_pci.c | 67 ++++++++++++++++++----------------- tools/libs/light/libxl_pcid.c | 38 ++++++++++++++++++++ 3 files changed, 90 insertions(+), 32 deletions(-) diff --git a/tools/include/xen-pcid.h b/tools/include/xen-pcid.h index ad2dec80df..e731340653 100644 --- a/tools/include/xen-pcid.h +++ b/tools/include/xen-pcid.h @@ -182,6 +182,23 @@ #define PCID_RESULT_KEY_IOMEM "iomem" #define PCID_RESULT_KEY_IRQS "irqs" +/* + ******************************************************************************* + * Write BDF values to the pciback sysfs path + * + * This command resets PCI device + * + * Request (see other mandatory fields above): + * - "cmd" field of the request must be set to "write_bdf". + * - "sbdf" SBDF of the device in format defined by PCID_SBDF_FMT. + * - "name" name of sysfs file of pciback driver + * + * Response (see other mandatory fields above): + * - "resp" field of the response must be set to "write_bdf". + */ +#define PCID_CMD_WRITE_BDF "write_bdf" +#define PCID_MSG_FIELD_NAME "name" + /* ******************************************************************************* * Reset PCI device diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c index 955635125f..d771f587c5 100644 --- a/tools/libs/light/libxl_pci.c +++ b/tools/libs/light/libxl_pci.c @@ -94,6 +94,8 @@ static int pci_handle_response(libxl__gc *gc, else if (strcmp(command_name, PCID_CMD_RESOURCE_LIST) == 0) *result = (libxl__json_object *)libxl__json_map_get(PCID_MSG_FIELD_RESOURCES, response, JSON_MAP); + else if (strcmp(command_name, PCID_CMD_WRITE_BDF) == 0) + *result = libxl__json_object_alloc(gc, JSON_NULL); return ret; } @@ -511,33 +513,6 @@ static bool is_pci_in_array(libxl_device_pci *pcis, int num, return i < num; } -/* Write the standard BDF into the sysfs path given by sysfs_path. */ -static int sysfs_write_bdf(libxl__gc *gc, const char * sysfs_path, - libxl_device_pci *pci) -{ - int rc, fd; - char *buf; - - fd = open(sysfs_path, O_WRONLY); - if (fd < 0) { - LOGE(ERROR, "Couldn't open %s", sysfs_path); - return ERROR_FAIL; - } - - buf = GCSPRINTF(PCI_BDF, pci->domain, pci->bus, - pci->dev, pci->func); - rc = write(fd, buf, strlen(buf)); - /* Annoying to have two if's, but we need the errno */ - if (rc < 0) - LOGE(ERROR, "write to %s returned %d", sysfs_path, rc); - close(fd); - - if (rc < 0) - return ERROR_FAIL; - - return 0; -} - #define PCI_INFO_PATH "/libxl/pci" static char *pci_info_xs_path(libxl__gc *gc, libxl_device_pci *pci, @@ -1384,6 +1359,36 @@ static bool pci_supp_legacy_irq(void) #endif } +static int pciback_write_bdf(libxl__gc *gc, char *name, libxl_device_pci *pci) +{ + struct vchan_info *vchan; + int rc; + libxl__json_object *args, *result; + + vchan = pci_vchan_get_client(gc); + if (!vchan) { + rc = ERROR_NOT_READY; + goto out; + } + + args = libxl__vchan_start_args(gc); + + libxl__vchan_arg_add_string(gc, args, PCID_MSG_FIELD_SBDF, + GCSPRINTF(PCID_SBDF_FMT, pci->domain, + pci->bus, pci->dev, pci->func)); + libxl__vchan_arg_add_string(gc, args, PCID_MSG_FIELD_NAME, name); + + result = vchan_send_command(gc, vchan, PCID_CMD_WRITE_BDF, args); + if (!result) { + rc = ERROR_FAIL; + goto vchan_free; + } +vchan_free: + pci_vchan_free(gc, vchan); +out: + return rc; +} + static void pci_add_dm_done(libxl__egc *egc, pci_add_state *pas, int rc) @@ -1421,8 +1426,9 @@ static void pci_add_dm_done(libxl__egc *egc, libxl__vchan_arg_add_integer(gc, args, PCID_MSG_FIELD_DOMID, domid); result = vchan_send_command(gc, vchan, PCID_CMD_RESOURCE_LIST, args); + pci_vchan_free(gc, vchan); if (!result) - goto vchan_free; + goto out; value = libxl__json_map_get(PCID_RESULT_KEY_IOMEM, result, JSON_ARRAY); /* stubdomain is always running by now, even at create time */ @@ -1483,8 +1489,7 @@ static void pci_add_dm_done(libxl__egc *egc, /* Don't restrict writes to the PCI config space from this VM */ if (pci->permissive) { - if ( sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER"/permissive", - pci) < 0 ) { + if (pciback_write_bdf(gc, "permissive", pci)) { LOGD(ERROR, domainid, "Setting permissive for device"); rc = ERROR_FAIL; goto out; @@ -1512,8 +1517,6 @@ out_no_irq: rc = libxl__device_pci_add_xenstore(gc, domid, pci, starting); else rc = 0; -vchan_free: - pci_vchan_free(gc, vchan); out: libxl__ev_time_deregister(gc, &pas->timeout); libxl__ev_time_deregister(gc, &pas->timeout_retries); diff --git a/tools/libs/light/libxl_pcid.c b/tools/libs/light/libxl_pcid.c index 0befb1999d..9368ba6c73 100644 --- a/tools/libs/light/libxl_pcid.c +++ b/tools/libs/light/libxl_pcid.c @@ -257,6 +257,41 @@ static int pciback_dev_is_assigned(libxl__gc *gc, unsigned int domain, return 0; } +static int process_pciback_write_bdf(libxl__gc *gc, yajl_gen gen, + char *command_name, + const struct libxl__json_object *request, + struct libxl__json_object **response) +{ + const struct libxl__json_object *json_o; + unsigned int dom, bus, dev, func; + int rc = 0; + const char *name; + char *spath; + + json_o = libxl__json_map_get(PCID_MSG_FIELD_SBDF, request, JSON_STRING); + if (!json_o) { + make_error_reply(gc, gen, "No mandatory parameter 'sbdf'", command_name); + return ERROR_FAIL; + } + + if (sscanf(libxl__json_object_get_string(json_o), PCID_SBDF_FMT, + &dom, &bus, &dev, &func) != 4) { + make_error_reply(gc, gen, "Can't parse SBDF", command_name); + return ERROR_FAIL; + } + + json_o = libxl__json_map_get(PCID_MSG_FIELD_NAME, request, JSON_STRING); + if (!json_o) { + make_error_reply(gc, gen, "No mandatory parameter 'rebind'", command_name); + return ERROR_FAIL; + } + + name = libxl__json_object_get_string(json_o); + spath = GCSPRINTF("%s/%s", SYSFS_PCIBACK_DRIVER, name); + LOG(WARN, "sysf_write_bdf(%s, %d, %d, %d, %d)", spath, dom, bus, dev,func); + return rc; +} + #define PCID_INFO_PATH "pcid" #define PCID_BDF_XSPATH "%04x-%02x-%02x-%01x" @@ -746,6 +781,9 @@ static int pcid_handle_request(libxl__gc *gc, yajl_gen gen, else if (strcmp(command_name, PCID_CMD_RESOURCE_LIST) == 0) ret = process_list_resources(gc, gen, command_name, request, &command_response); + else if (strcmp(command_name, PCID_CMD_WRITE_BDF) == 0) + ret = process_pciback_write_bdf(gc, gen, command_name, + request, &command_response); else { /* * This is an unsupported command: make a reply and proceed over