From nobody Mon Feb 9 01:30:54 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1675958219; cv=none; d=zohomail.com; s=zohoarc; b=foUM9pJEX0Jlgyo+n8KKBeWmvm3WcCIE75bKP+1Jp3+pdRjKC1JEjHPjJdQ9mvjaXZ07lAYTu0WXtAhcKMVy7HOZ+TfzUG0A9QWJCwUJGbqeTyxpV4Wy8vhNdgwfDN6Onmj1Tao0n7S8r4jB6QZh5sVBIZIyCdvG0sr0Z2CCvO0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1675958219; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=bmsCcMttxSYbw0pYCwcBt+l3AXaiynpdRsxaUXNJnFU=; b=LJZUH8I7cU4Nb35M4mEOUnMcRfLwIfdpyDlmZIYeCfC95J6QFdtBhP2i9V+fz/AAurjdcnNK2x9yhgo4tSIMSsYZfWvP+I7YPVpjyXDr11+5XGLcdOcYzFd/QegVpYW7Kfzkc36d5GfziuNKiTPCFLAXxPWbkxA5yMmb+/hfPow= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1675958219969529.5304439299802; Thu, 9 Feb 2023 07:56:59 -0800 (PST) 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 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-Outflank-Mailman: Message body and most headers restored to incoming version 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 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1675958221437100003 Content-Type: text/plain; charset="utf-8" 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 @@ =20 #define PCID_SBDF_FMT "%04x:%02x:%02x.%01x" =20 +/* + *************************************************************************= ****** + * List assignable devices + * + * This command lists PCI devices that can be passed through to a guest do= main. + * + * 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); =20 #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" =20 +static int process_list_assignable(libxl__gc *gc, + const libxl__json_object *response, + libxl__json_object **result) +{ + *result =3D (libxl__json_object *)libxl__json_map_get(PCID_MSG_FIELD_D= EVICES, + response, JSON_ARR= AY); + 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 =3D command_obj->u.string; LOG(DEBUG, "command: %s", command_name); =20 + if (strcmp(command_name, PCID_CMD_LIST_ASSIGNABLE) =3D=3D 0) + ret =3D process_list_assignable(gc, response, result); + return ret; } =20 @@ -124,8 +139,7 @@ static char *pci_prepare_request(libxl__gc *gc, yajl_ge= n gen, char *cmd, return request; } =20 -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; =20 @@ -147,8 +161,7 @@ out: return vchan; } =20 -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(li= bxl_ctx *ctx, int *num) { GC_INIT(ctx); libxl_device_pci *pcis =3D NULL, *new; - struct dirent *de; - DIR *dir; + struct vchan_info *vchan; + libxl__json_object *result, *dev_obj; + int i; =20 *num =3D 0; =20 - dir =3D opendir(SYSFS_PCIBACK_DRIVER); - if (NULL =3D=3D dir) { - if (errno =3D=3D ENOENT) { - LOG(ERROR, "Looks like pciback driver not loaded"); - } else { - LOGE(ERROR, "Couldn't open %s", SYSFS_PCIBACK_DRIVER); - } + vchan =3D pci_vchan_get_client(gc); + if (!vchan) goto out; - } =20 - while((de =3D readdir(dir))) { + result =3D vchan_send_command(gc, vchan, PCID_CMD_LIST_ASSIGNABLE, NUL= L); + if (!result) + goto vchan_free; + + for (i =3D 0; (dev_obj =3D libxl__json_array_get(result, i)); i++) { + const char *sbdf_str =3D libxl__json_object_get_string(dev_obj); unsigned int dom, bus, dev, func; - char *name; + const char *name; + + if (!sbdf_str) + continue; =20 - if (sscanf(de->d_name, PCI_BDF, &dom, &bus, &dev, &func) !=3D 4) + if (sscanf(sbdf_str, PCID_SBDF_FMT, &dom, &bus, &dev, &func) !=3D = 4) continue; =20 new =3D realloc(pcis, ((*num) + 1) * sizeof(*new)); @@ -602,7 +618,9 @@ libxl_device_pci *libxl_device_pci_assignable_list(libx= l_ctx *ctx, int *num) (*num)++; } =20 - 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; } =20 +static int process_list_assignable(libxl__gc *gc, yajl_gen gen, + char *command_name, + const struct libxl__json_object *reques= t, + struct libxl__json_object **response) +{ + struct dirent *de; + DIR *dir =3D NULL; + + dir =3D opendir(SYSFS_PCI_DEV); + if (dir =3D=3D NULL) { + make_error_reply(gc, gen, strerror(errno), command_name); + return ERROR_FAIL; + } + + libxl__yajl_gen_asciiz(gen, PCID_MSG_FIELD_DEVICES); + + *response =3D libxl__json_object_alloc(gc, JSON_ARRAY); + + while ((de =3D readdir(dir))) { + unsigned int dom, bus, dev, func; + + if (sscanf(de->d_name, PCID_SBDF_FMT, &dom, &bus, &dev, &func) != =3D 4) + continue; + + struct libxl__json_object *node =3D + libxl__json_object_alloc(gc, JSON_STRING); + node->u.string =3D 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_ge= n gen, =20 command_name =3D command_obj->u.string; =20 - /* - * This is an unsupported command: make a reply and proceed over - * the error path. - */ - ret =3D make_error_reply(gc, gen, "Unsupported command", - command_name); - if (!ret) - ret =3D ERROR_NOTFOUND; + if (strcmp(command_name, PCID_CMD_LIST_ASSIGNABLE) =3D=3D 0) + ret =3D 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 =3D make_error_reply(gc, gen, "Unsupported command", + command_name); + if (!ret) + ret =3D ERROR_NOTFOUND; + } =20 if (ret) { /* --=20 2.34.1