From nobody Thu May 2 11:40:14 2024 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=1628778185; cv=none; d=zohomail.com; s=zohoarc; b=fHRX0WV9MixSeeYT2obpVnOKUsrmdRN5+KFKT/UE19KM7SapCC/a+iKw//LWltcQGQq1p3/icRS1kt3bQ/oDKD0Efbp1PXP8IOeJH/uHD+fpaMlx71txAKfV1Z7TJmxzFyyeQ2WhOqGq/T6gD9IZj5XUlbIH0z6g62kn32pqbO8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628778185; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=oJL+jaD3j+tB2hFVXmAv2nPtE4uqfFnnjmMp4UudG8E=; b=m6XhKOuVloqVHgPfh1gr3hlMf4T4KrEm730G2DKkq0TI60atW2X88v2Dwd4DPLhkHklYBIekRzaIuCO/is4Pg5uXT+vLzNlUzw6ejhW9xPnZ65lZwGFsaIoWAw33FpZNA97clePBEqngXQncyVNe6CWUgq70gXWPaAmC7GHAUsY= 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 162877818587818.146968373757886; Thu, 12 Aug 2021 07:23:05 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.166457.303800 (Exim 4.92) (envelope-from ) id 1mEBbW-0002hQ-ST; Thu, 12 Aug 2021 14:22:42 +0000 Received: by outflank-mailman (output) from mailman id 166457.303800; Thu, 12 Aug 2021 14:22:42 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mEBbW-0002hJ-PB; Thu, 12 Aug 2021 14:22:42 +0000 Received: by outflank-mailman (input) for mailman id 166457; Thu, 12 Aug 2021 14:22:41 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mEBbU-0002Q2-Ug for xen-devel@lists.xenproject.org; Thu, 12 Aug 2021 14:22:40 +0000 Received: from mail-lf1-x133.google.com (unknown [2a00:1450:4864:20::133]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 04f3e53c-e01f-4b37-950c-1653ded55785; Thu, 12 Aug 2021 14:22:36 +0000 (UTC) Received: by mail-lf1-x133.google.com with SMTP id r9so4320564lfn.3 for ; Thu, 12 Aug 2021 07:22:36 -0700 (PDT) Received: from localhost.localdomain (host-176-36-213-80.b024.la.net.ua. [176.36.213.80]) by smtp.gmail.com with ESMTPSA id s16sm329678ljj.48.2021.08.12.07.22.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Aug 2021 07:22:33 -0700 (PDT) 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: 04f3e53c-e01f-4b37-950c-1653ded55785 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=oJL+jaD3j+tB2hFVXmAv2nPtE4uqfFnnjmMp4UudG8E=; b=Ok0YewcvgGM4yeJGorn27Lv/uQbCmYa+oYSKajj4ZrE5uDL9uGeWVcjQljD3XjBDQ+ sKaUSJX93gXiRpO3QAB5afdUc1IjgkENfwYx5TYrAVDvzeHo3LM2PGbckFyFrpiLMcEr fEUTVs09hQjcgdT2UdNdEd5IgEzop3H+qDiBQSH6csiwegggo5sOt/6qKcv0Z54/rWxs hGpze9dvH8YwgaM+CGuX5PtVQcwPU/IahzIQpw0s+w+3bQRqR9MbmB/yt1yEWjb1nXJ4 aGGiDAePxeXv2Zt2JcaWKCnhijOFYPVq+wr+f+cg26ol4sk9JFeLg1Zw4RAc30dtEFJX JVIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=oJL+jaD3j+tB2hFVXmAv2nPtE4uqfFnnjmMp4UudG8E=; b=taHVG89kwMVDIVsN/rxz2DVPIrKNimMhHCQQxzEIjP8FGXfJ5WL35oRpM1PNOXrI36 obWi7v5xagPRHLzdfudGkpF/in+e0Dm7kcSDG5w9vDLov00R8liyRJ3cFQBPa5IGZABc 2lrys7JvW4FHic+2MWH9cHjZWxId4h4QhRWUdsc/iNP2ClvxWP8zD2GMdP7MWaVIuBlh yl6wyYGRKWPOK3+j8WFPKxDVlwloMnw/KOWDN868U4qPjG2DmZPVqzIUdIbqm9busScS sfGyTfwagPHlONPqQBZgTndWBMYoY4n1xQnTVFqKaoeXO/9yVwJcxByiTce+eMDmFQmq HqnA== X-Gm-Message-State: AOAM531LAH096+ZB3m9JQREunILtc/jEr808PbZMoLZdROtFJvZo4OzC 0nrLvQSFYvUIl0rmVi902oFz2P3StyyXWA== X-Google-Smtp-Source: ABdhPJzTKbIO03oj9tadXceoS/j+YjCsBYKcGbGLpbBoMkCEdQJKKVTNO8JK8n9uGEbU70Ysc2xF+g== X-Received: by 2002:a05:6512:1148:: with SMTP id m8mr2664586lfg.53.1628778153388; Thu, 12 Aug 2021 07:22:33 -0700 (PDT) From: Anastasiia Lukianenko To: xen-devel@lists.xenproject.org Cc: oleksandr_tyshchenko@epam.com, artem_mygaiev@epam.com, rahul.singh@arm.com, bertrand.marquis@arm.com, oleksandr_andrushchenko@epam.com, roger.pau@citrix.com, julien@xen.org, sstabellini@kernel.org, andr2000@gmail.com, vicooodin@gmail.com, Anastasiia Lukianenko Subject: [PATCH 1/9] tools/libs/light: Add vchan support to libxl Date: Thu, 12 Aug 2021 17:22:22 +0300 Message-Id: <20210812142230.19353-2-vicooodin@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210812142230.19353-1-vicooodin@gmail.com> References: <20210812142230.19353-1-vicooodin@gmail.com> X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628778187093100005 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Anastasiia Lukianenko 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 --- tools/helpers/Makefile | 4 +- tools/libs/light/Makefile | 7 +- tools/libs/light/libxl_vchan.c | 359 +++++++++++++++++++++++++++++++++ tools/libs/light/libxl_vchan.h | 81 ++++++++ tools/xl/Makefile | 3 +- 5 files changed, 448 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 1bcc97ea8a..ad2cdf32ad 100644 --- a/tools/helpers/Makefile +++ b/tools/helpers/Makefile @@ -29,12 +29,12 @@ $(INIT_XENSTORE_DOMAIN_OBJS): CFLAGS +=3D $(CFLAGS_libx= enlight) all: $(PROGS) =20 xen-init-dom0: $(XEN_INIT_DOM0_OBJS) - $(CC) $(LDFLAGS) -o $@ $(XEN_INIT_DOM0_OBJS) $(LDLIBS_libxenctrl) $(LDLIB= S_libxentoollog) $(LDLIBS_libxenstore) $(LDLIBS_libxenlight) $(APPEND_LDFLA= GS) + $(CC) $(LDFLAGS) -o $@ $(XEN_INIT_DOM0_OBJS) $(LDLIBS_libxenctrl) $(LDLIB= S_libxentoollog) $(LDLIBS_libxenstore) $(LDLIBS_libxenlight) $(LDLIBS_libxe= nvchan) $(APPEND_LDFLAGS) =20 $(INIT_XENSTORE_DOMAIN_OBJS): _paths.h =20 init-xenstore-domain: $(INIT_XENSTORE_DOMAIN_OBJS) - $(CC) $(LDFLAGS) -o $@ $(INIT_XENSTORE_DOMAIN_OBJS) $(LDLIBS_libxentoollo= g) $(LDLIBS_libxenstore) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIB= S_libxenlight) $(APPEND_LDFLAGS) + $(CC) $(LDFLAGS) -o $@ $(INIT_XENSTORE_DOMAIN_OBJS) $(LDLIBS_libxentoollo= g) $(LDLIBS_libxenstore) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIB= S_libxenlight) $(LDLIBS_libxenvchan) $(APPEND_LDFLAGS) =20 .PHONY: install install: all diff --git a/tools/libs/light/Makefile b/tools/libs/light/Makefile index 4a4de12610..f5d34b3371 100644 --- a/tools/libs/light/Makefile +++ b/tools/libs/light/Makefile @@ -76,6 +76,7 @@ SRCS-y +=3D libxl.c SRCS-y +=3D libxl_create.c SRCS-y +=3D libxl_dm.c SRCS-y +=3D libxl_pci.c +SRCS-y +=3D libxl_vchan.c SRCS-y +=3D libxl_dom.c SRCS-y +=3D libxl_exec.c SRCS-y +=3D libxl_xshelp.c @@ -176,7 +177,7 @@ LDUSELIBS-y +=3D $(PTHREAD_LIBS) LDUSELIBS-y +=3D -lyajl LDUSELIBS +=3D $(LDUSELIBS-y) =20 -$(LIB_OBJS) $(PIC_OBJS) $(LIBXL_TEST_OBJS): CFLAGS +=3D $(CFLAGS_LIBXL) -i= nclude $(XEN_ROOT)/tools/config.h +$(LIB_OBJS) $(PIC_OBJS) $(LIBXL_TEST_OBJS): CFLAGS +=3D $(CFLAGS_LIBXL) $(= CFLAGS_libxenevtchn) -include $(XEN_ROOT)/tools/config.h $(ACPI_OBJS) $(ACPI_PIC_OBJS): CFLAGS +=3D -I. -DLIBACPI_STDUTILS=3D\"$(CU= RDIR)/libxl_x86_acpi.h\" $(TEST_PROG_OBJS) _libxl.api-for-check: CFLAGS +=3D $(CFLAGS_libxentoollog= ) $(CFLAGS_libxentoolcore) libxl_dom.o libxl_dom.opic: CFLAGS +=3D -I$(XEN_ROOT)/tools # include lib= acpi/x86.h @@ -240,13 +241,13 @@ libxenlight_test.so: $(PIC_OBJS) $(LIBXL_TEST_OBJS) $(CC) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenlight.so.$(MAJOR) $(SHLIB= _LDFLAGS) -o $@ $^ $(LDUSELIBS) $(APPEND_LDFLAGS) =20 test_%: test_%.o test_common.o libxenlight_test.so - $(CC) $(LDFLAGS) -o $@ $^ $(filter-out %libxenlight.so, $(LDLIBS_libxenli= ght)) $(LDLIBS_libxentoollog) $(LDLIBS_libxentoolcore) -lyajl $(APPEND_LDFL= AGS) + $(CC) $(LDFLAGS) -o $@ $^ $(filter-out %libxenlight.so, $(LDLIBS_libxenli= ght)) $(LDLIBS_libxentoollog) $(LDLIBS_libxentoolcore) $(LDLIBS_libxenvchan= ) -lyajl $(APPEND_LDFLAGS) =20 libxl-save-helper: $(SAVE_HELPER_OBJS) libxenlight.so $(CC) $(LDFLAGS) -o $@ $(SAVE_HELPER_OBJS) $(LDLIBS_libxentoollog) $(LDLI= BS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxentoolcore) $(APPEND_LDFL= AGS) =20 testidl: testidl.o libxenlight.so - $(CC) $(LDFLAGS) -o $@ testidl.o $(LDLIBS_libxenlight) $(LDLIBS_libxentoo= llog) $(LDLIBS_libxentoolcore) $(APPEND_LDFLAGS) + $(CC) $(LDFLAGS) -o $@ testidl.o $(LDLIBS_libxenlight) $(LDLIBS_libxentoo= llog) $(LDLIBS_libxentoolcore) $(LDLIBS_libxenvchan) $(APPEND_LDFLAGS) =20 install: installlocal $(LIBHEADERS) =20 diff --git a/tools/libs/light/libxl_vchan.c b/tools/libs/light/libxl_vchan.c new file mode 100644 index 0000000000..b533243fd3 --- /dev/null +++ b/tools/libs/light/libxl_vchan.c @@ -0,0 +1,359 @@ +/* + * Vchan support for JSON messages processing + * + * Copyright (C) 2021 EPAM Systems Inc. + * Author: Oleksandr Andrushchenko + * Author: Anastasiia Lukianenko + * + * 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" + +/* Based on QMP Parameters Helpers */ +static void vchan_parameters_common_add(libxl__gc *gc, libxl__json_object = **param, + const char *name, libxl__json_obje= ct *obj) +{ + libxl__json_map_node *arg =3D NULL; + + if (!*param) { + *param =3D libxl__json_object_alloc(gc, JSON_MAP); + } + + GCNEW(arg); + + arg->map_key =3D libxl__strdup(gc, name); + arg->obj =3D obj; + + flexarray_append((*param)->u.map, arg); +} + + +void libxl__vchan_param_add_string(libxl__gc *gc, libxl__json_object **par= am, + const char *name, const char *s) +{ + libxl__json_object *obj; + + obj =3D libxl__json_object_alloc(gc, JSON_STRING); + obj->u.string =3D libxl__strdup(gc, s); + + vchan_parameters_common_add(gc, param, name, obj); +} + +void libxl__vchan_param_add_integer(libxl__gc *gc, libxl__json_object **pa= ram, + const char *name, const long long i) +{ + libxl__json_object *obj; + + obj =3D libxl__json_object_alloc(gc, JSON_INTEGER); + obj->u.i =3D i; + + vchan_parameters_common_add(gc, param, name, obj); +} + +/* Returns 1 if path exists, 0 if not, ERROR_* (<0) on error. */ +int xs_path_exists(libxl__gc *gc, const char *xs_path) +{ + int rc; + const char *dir; + + rc =3D libxl__xs_read_checked(gc, XBT_NULL, xs_path, &dir); + if (rc) + return rc; + if (dir) + return 1; + return 0; +} + +libxl_domid vchan_find_server(libxl__gc *gc, char *xs_dir, char *xs_path) +{ + char **domains; + unsigned int i, n; + libxl_domid domid =3D DOMID_INVALID; + + domains =3D libxl__xs_directory(gc, XBT_NULL, "/local/domain", &n); + if (!n) + goto out; + + for (i =3D 0; i < n; i++) { + int d; + + if (sscanf(domains[i], "%d", &d) !=3D 1) + continue; + if (xs_path_exists(gc, GCSPRINTF("%s%d%s", xs_dir, d, xs_path)) > = 0) { + /* Found the domain where the server lives. */ + domid =3D d; + break; + } + } + +out: + return domid; +} + +static int vchan_init_client(libxl__gc *gc, struct vchan_state *state, int= is_server) +{ + if (is_server) { + state->ctrl =3D libxenvchan_server_init(NULL, state->domid, state->xs_pa= th, 0, 0); + if (!state->ctrl) { + perror("Libxenvchan server init failed\n"); + exit(1); + } + } else { + state->ctrl =3D libxenvchan_client_init(CTX->lg, state->domid, + state->xs_path); + if (!state->ctrl) { + LOGE(ERROR, "Couldn't intialize vchan client"); + return ERROR_FAIL; + } + } + + state->select_fd =3D 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, "Intialized vchan client, server at %s", state->xs_path); + + return 0; +} + +/* + * TODO: Running this code in multi-threaded environment + * The code now assumes that there is only one client invocation process + * in one domain. In the future, it is necessary to take into account cases + * when within one domain there will be several requests from a client at = the + * same time. Therefore, it will be necessary to regulate the multithreadi= ng + * of processes. + */ +struct vchan_state *vchan_get_instance(libxl__gc *gc, libxl_domid domid, + char *vchan_xs_path, int is_server) +{ + static struct vchan_state *state =3D NULL; + int ret; + + if (state) + return state; + + state =3D libxl__zalloc(gc, sizeof(*state)); + state->domid =3D domid; + state->xs_path =3D vchan_xs_path; + ret =3D vchan_init_client(gc, state, is_server); + if (ret) + state =3D NULL; + + return state; +} + +/* + * 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 =3D NULL; + const size_t eoml =3D sizeof(END_OF_MESSAGE) - 1; + libxl__json_object *o =3D NULL; + + if (!state->rx_buf_used) + return ERROR_NOTFOUND; + + /* Search for the end of a message: "\r\n" */ + end =3D memmem(state->rx_buf, state->rx_buf_used, END_OF_MESSAGE, eoml= ); + if (!end) + return ERROR_NOTFOUND; + len =3D (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] =3D '\0'; + o =3D libxl__json_parse(gc, state->rx_buf); + + if (!o) { + LOGD(ERROR, state->domid, "Parse error"); + return ERROR_FAIL; + } + + state->rx_buf_used -=3D len; + memmove(state->rx_buf, state->rx_buf + len, state->rx_buf_used); + + LOGD(DEBUG, state->domid, "JSON object received: %s", JSON(o)); + + *o_r =3D o; + + return 0; +} + +static libxl__json_object *vchan_handle_message(libxl__gc *gc, + struct vchan_info *vchan, + const libxl__json_object *= request) +{ + libxl__json_object *result =3D NULL; + const libxl__json_object *command_obj; + int ret; + + ret =3D vchan->handle_msg(gc, request, &result); + if (ret =3D=3D ERROR_FAIL) { + LOGE(ERROR, "Message handling failed\n"); + } else if (ret =3D=3D ERROR_NOTFOUND) { + command_obj =3D libxl__json_map_get(VCHAN_MSG_EXECUTE, request, JSON_ANY= ); + LOGE(ERROR, "Unknown command: %s\n", command_obj->u.string); + } + return result; +} + +static int set_nonblocking(int fd, int nonblocking) +{ + int flags =3D fcntl(fd, F_GETFL); + if (flags =3D=3D -1) + return -1; + + if (nonblocking) + flags |=3D O_NONBLOCK; + else + flags &=3D ~O_NONBLOCK; + + if (fcntl(fd, F_SETFL, flags) =3D=3D -1) + return -1; + + return 0; +} + +static libxl__json_object *vchan_process_request(libxl__gc *gc, + struct vchan_info *vchan) +{ + int rc, ret; + ssize_t r; + fd_set rfds; + fd_set wfds; + + while (true) { + FD_ZERO(&rfds); + FD_ZERO(&wfds); + FD_SET(vchan->state->select_fd, &rfds); + ret =3D select(vchan->state->select_fd + 1, &rfds, &wfds, NULL, NU= LL); + if (ret < 0) { + LOGE(ERROR, "Error occured during the libxenvchan fd monitorin= g\n"); + return NULL; + } + if (FD_ISSET(vchan->state->select_fd, &rfds)) + libxenvchan_wait(vchan->state->ctrl); + /* Check if the buffer still have space, or increase size */ + if (vchan->state->rx_buf_size - vchan->state->rx_buf_used < vchan-= >receive_buf_size) { + size_t newsize =3D vchan->state->rx_buf_size * 2 + vchan->rece= ive_buf_size; + + if (newsize > vchan->max_buf_size) { + LOGD(ERROR, vchan->state->domid, + "receive buffer is too big (%zu > %zu)", + newsize, vchan->max_buf_size); + return NULL; + } + vchan->state->rx_buf_size =3D newsize; + vchan->state->rx_buf =3D libxl__realloc(gc, vchan->state->rx_b= uf, + vchan->state->rx_buf_siz= e); + } + + while (libxenvchan_data_ready(vchan->state->ctrl)) { + r =3D libxenvchan_read(vchan->state->ctrl, + vchan->state->rx_buf + vchan->state->rx_b= uf_used, + vchan->state->rx_buf_size - vchan->state-= >rx_buf_used); + if (r < 0) { + LOGED(ERROR, vchan->state->domid, "error reading"); + return NULL; + } + + LOG(DEBUG, "received %zdB: '%.*s'", r, + (int)r, vchan->state->rx_buf + vchan->state->rx_buf_used); + + vchan->state->rx_buf_used +=3D r; + assert(vchan->state->rx_buf_used <=3D vchan->state->rx_buf_siz= e); + + libxl__json_object *o =3D NULL; + /* parse rx buffer to find one json object */ + rc =3D vchan_get_next_msg(gc, vchan->state, &o); + if (rc =3D=3D ERROR_NOTFOUND) + break; + else if (rc) + return NULL; + + return vchan_handle_message(gc, vchan, o); + } + if ( !libxenvchan_is_open(vchan->state->ctrl)) { + if (set_nonblocking(1, 0)) + return NULL; + } + } + return NULL; +} + +static int vchan_write(libxl__gc *gc, struct vchan_state *state, char *cmd) +{ + size_t len; + int ret; + + len =3D strlen(cmd); + while (len) { + ret =3D libxenvchan_write(state->ctrl, cmd, len); + if (ret < 0) { + LOGE(ERROR, "vchan write failed"); + return ERROR_FAIL; + } + cmd +=3D ret; + len -=3D ret; + } + return 0; +} + +libxl__json_object *vchan_send_command(libxl__gc *gc, struct vchan_info *v= chan, + const char *cmd, libxl__json_object= *args) +{ + libxl__json_object *result; + char *json; + int ret; + + json =3D vchan->prepare_cmd(gc, cmd, args, 0); + if (!json) + return NULL; + + ret =3D vchan_write(gc, vchan->state, json); + if (ret < 0) + return NULL; + + result =3D vchan_process_request(gc, vchan); + return result; +} + +int vchan_process_command(libxl__gc *gc, struct vchan_info *vchan) +{ + libxl__json_object *result; + char *json; + int ret; + + result =3D vchan_process_request(gc, vchan); + + json =3D vchan->prepare_cmd(gc, NULL, result, 0); + if (!json) + return -1; + + ret =3D vchan_write(gc, vchan->state, json); + if (ret < 0) + return -1; + + return 0; +} diff --git a/tools/libs/light/libxl_vchan.h b/tools/libs/light/libxl_vchan.h new file mode 100644 index 0000000000..7ded416447 --- /dev/null +++ b/tools/libs/light/libxl_vchan.h @@ -0,0 +1,81 @@ +/* + 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 + +#define VCHAN_SERVER 1 +#define VCHAN_CLIENT 0 + +#define END_OF_MESSAGE "\r\n" + +#define VCHAN_MSG_EXECUTE "execute" +#define VCHAN_MSG_RETURN "return" +#define VCHAN_MSG_ERROR "error" + +struct vchan_state { + /* Server domain ID. */ + libxl_domid domid; + /* XenStore path of the server with the ring buffer and event channel.= */ + char *xs_path; + struct libxenvchan *ctrl; + int select_fd; + /* receive buffer */ + char *rx_buf; + size_t rx_buf_size; /* current allocated size */ + size_t rx_buf_used; /* actual data in the buffer */ +}; + +typedef int (*vchan_handle_t)(libxl__gc *gc, const libxl__json_object *req= uest, + libxl__json_object **result); +typedef char* (*vchan_prepare_t)(libxl__gc *gc, const char *cmd, + libxl__json_object *args, int id); +struct vchan_info { + struct vchan_state *state; + vchan_handle_t handle_msg; + vchan_prepare_t prepare_cmd; + /* buffer info */ + size_t receive_buf_size; + size_t max_buf_size; +}; + +void libxl__vchan_param_add_string(libxl__gc *gc, libxl__json_object **par= am, + const char *name, const char *s); +void libxl__vchan_param_add_integer(libxl__gc *gc, libxl__json_object **pa= ram, + const char *name, const long long i); +int xs_path_exists(libxl__gc *gc, const char *xs_path); +libxl_domid vchan_find_server(libxl__gc *gc, char *xs_dir, char *xs_path); +struct vchan_state *vchan_get_instance(libxl__gc *gc, libxl_domid domid, + char *vchan_xs_path, int is_server); +libxl__json_object *vchan_send_command(libxl__gc *gc, struct vchan_info *v= chan, + const char *cmd, libxl__json_object= *args); +int vchan_process_command(libxl__gc *gc, struct vchan_info *vchan); + +#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 a5d2ee4528..d35570c343 100644 --- a/tools/xl/Makefile +++ b/tools/xl/Makefile @@ -15,6 +15,7 @@ LDFLAGS +=3D $(PTHREAD_LDFLAGS) CFLAGS_XL +=3D $(CFLAGS_libxenlight) CFLAGS_XL +=3D $(CFLAGS_libxenutil) CFLAGS_XL +=3D -Wshadow +CFLAGS_XL +=3D $(CFLAGS_libxenvchan) =20 XL_OBJS-$(CONFIG_X86) =3D xl_psr.o XL_OBJS =3D xl.o xl_cmdtable.o xl_sxp.o xl_utils.o $(XL_OBJS-y) @@ -38,7 +39,7 @@ $(XL_OBJS): _paths.h all: xl =20 xl: $(XL_OBJS) - $(CC) $(LDFLAGS) -o $@ $(XL_OBJS) $(LDLIBS_libxenutil) $(LDLIBS_libxenlig= ht) $(LDLIBS_libxentoollog) -lyajl $(APPEND_LDFLAGS) + $(CC) $(LDFLAGS) -o $@ $(XL_OBJS) $(LDLIBS_libxenutil) $(LDLIBS_libxenlig= ht) $(LDLIBS_libxentoollog) $(LDLIBS_libxenvchan) -lyajl $(APPEND_LDFLAGS) =20 .PHONY: install install: all --=20 2.17.1 From nobody Thu May 2 11:40:14 2024 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=1628778184; cv=none; d=zohomail.com; s=zohoarc; b=JRXlzm4m847ehef725A0bn9Fa9oCbclbxAfOvb8P74QOnblDsOT8wLiYq4VtIqdomj7iLeflhibfB/Iwt42iNhEvY93JccdB1Kgx7ZduDJUqxkKZJpIj8DDaTpNlWwSGO1uYhLWyWcBr5fesYUsDn9bah3LzBJah63hIiOmhYIQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628778184; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=JdPxHs8pmc5umX+oWUld/sGpQUemWUwM1K9E4JXG7Fw=; b=LZBo5R0HKfl+zSu+utscEXY+AHRUQ+MFif41RjHfxWd+Hircc1DnkNbYwJ4WCgb2QV7RSMhNWZpvr0mlTaUwybd97bEH4oEFk7wOHM1Aa7IUxDExTPYtyOwCjXkSCW/o5Jzx51i2XAqqYSXOqDUVLNBvS8dOlMA2qa7tgCr5TlU= 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 1628778184136538.8480865446395; Thu, 12 Aug 2021 07:23:04 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.166458.303811 (Exim 4.92) (envelope-from ) id 1mEBbb-00031H-7F; Thu, 12 Aug 2021 14:22:47 +0000 Received: by outflank-mailman (output) from mailman id 166458.303811; Thu, 12 Aug 2021 14:22:47 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mEBbb-00031A-1i; Thu, 12 Aug 2021 14:22:47 +0000 Received: by outflank-mailman (input) for mailman id 166458; Thu, 12 Aug 2021 14:22:46 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mEBbZ-0002Q2-Ur for xen-devel@lists.xenproject.org; Thu, 12 Aug 2021 14:22:45 +0000 Received: from mail-lf1-x135.google.com (unknown [2a00:1450:4864:20::135]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id dbfc7067-d493-430c-82db-cee0258dc3e7; Thu, 12 Aug 2021 14:22:37 +0000 (UTC) Received: by mail-lf1-x135.google.com with SMTP id z20so13830266lfd.2 for ; Thu, 12 Aug 2021 07:22:37 -0700 (PDT) Received: from localhost.localdomain (host-176-36-213-80.b024.la.net.ua. [176.36.213.80]) by smtp.gmail.com with ESMTPSA id s16sm329678ljj.48.2021.08.12.07.22.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Aug 2021 07:22:34 -0700 (PDT) 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: dbfc7067-d493-430c-82db-cee0258dc3e7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=JdPxHs8pmc5umX+oWUld/sGpQUemWUwM1K9E4JXG7Fw=; b=e4/bORrc7EIG5RcPhOR2s5nYwwOjF3V/SZY0Pg4tvMA7pSAB/dgfAngE1qul2z5JGV l43BDLzGd9wkdFllMqtAxBQxN7mtB72DpdJKWQji34l+QDFP/2iOTtA9cnKqIOjTam7j g/QxwV6EDe7rlE3tWs2ZLABjyoOKFDI8FbDDpc6OhsoCaMujRpZtaF6d5XbZYMZk/1/A LcRD+qKwg5lv3sOsC/MotURnrWJ6b62soupvU+fIZE629cO63DXHiZkjPoQ3Xx9p6UqY 4NAUs1bSRpegeQRGYKoTIqSEsUHARVWHLk7QXjZv/GJpHOB9lNuUlMmnKXEHJLTcKJR0 /FYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=JdPxHs8pmc5umX+oWUld/sGpQUemWUwM1K9E4JXG7Fw=; b=QhVxrrQYzMOCKlYKdnjO2HhTGz9oPcEAaz1JbbiARHqy0zvMC5aYNQHzqwI87Vydwy DS0sZ8rsKiSNmzVST2NCXwObbi7BA78ubXWA6PxvdD3FYrTFgsFzHc7z+v5VEWITKozp jmzYA7EpvkPinC9dGLap58afQqNEJcRV6QBJRR6ZCO0tLW/5L2nP56cMbs9QDkGNMO4+ nbKELU5jINT9RU310xKXBUA9sFrUAflNZ3VM3hsEB7HCboH5h17tgROyXqE/BmflBJhv mBzRpMEHma+NAlMW7dBCzqQJbqAmtxLXBes+eF/WF85BPBVVPjxQ0Wzvhei0eJZpygAr mQJw== X-Gm-Message-State: AOAM531xbKXJkGLyavOCPuIqFwzPgB0IiIY1BzLjvAwWbLJWttTQ3KxE yYSHBLL/owsmA0KxH9XOeMKlFbqjond+zg== X-Google-Smtp-Source: ABdhPJxP/uirSlE99zuR7nj0wlbr6O8tgDNBgu5mFUKVrxO12qW4mXOBZ2wrFGq31IKhpRu89hwanQ== X-Received: by 2002:ac2:53a8:: with SMTP id j8mr2940005lfh.477.1628778154463; Thu, 12 Aug 2021 07:22:34 -0700 (PDT) From: Anastasiia Lukianenko To: xen-devel@lists.xenproject.org Cc: oleksandr_tyshchenko@epam.com, artem_mygaiev@epam.com, rahul.singh@arm.com, bertrand.marquis@arm.com, oleksandr_andrushchenko@epam.com, roger.pau@citrix.com, julien@xen.org, sstabellini@kernel.org, andr2000@gmail.com, vicooodin@gmail.com, Anastasiia Lukianenko Subject: [PATCH 2/9] tools/libs/light: Add functions for handling PCI messages in JSON format Date: Thu, 12 Aug 2021 17:22:23 +0300 Message-Id: <20210812142230.19353-3-vicooodin@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210812142230.19353-1-vicooodin@gmail.com> References: <20210812142230.19353-1-vicooodin@gmail.com> X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628778185053100002 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Anastasiia Lukianenko These functions allow to process the received message from PCI server and to generate a reply in JSON format for the client. Signed-off-by: Anastasiia Lukianenko --- tools/include/pcid.h | 49 ++++++++ tools/libs/light/Makefile | 1 + tools/libs/light/libxl_pcid.c | 134 ++++++++++++++++++++++ tools/libs/light/libxl_types.idl | 1 + tools/libs/light/libxl_types_internal.idl | 6 + 5 files changed, 191 insertions(+) create mode 100644 tools/include/pcid.h create mode 100644 tools/libs/light/libxl_pcid.c diff --git a/tools/include/pcid.h b/tools/include/pcid.h new file mode 100644 index 0000000000..59a7aad64a --- /dev/null +++ b/tools/include/pcid.h @@ -0,0 +1,49 @@ +/* + 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. + + You should have received a copy of the GNU Lesser General Public + License along with this library; If not, see . +*/ + +#ifndef PCID_H +#define PCID_H + +#define PCID_XS_DIR "/local/domain/" +#define PCID_XS_PATH "/data/pcid-vchan" + +#define PCI_RECEIVE_BUFFER_SIZE 4096 +#define PCI_MAX_SIZE_RX_BUF MB(1) + +#define PCID_MSG_FIELD_ID "id" +#define PCID_MSG_FIELD_ARGS "arguments" + +#define PCID_PCIBACK_DRIVER "pciback_driver" + +#if defined(__linux__) +#define SYSFS_PCIBACK_DRIVER "/sys/bus/pci/drivers/pciback" +#endif + +int libxl_pcid_process(libxl_ctx *ctx); + +#endif /* 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 f5d34b3371..40e3a7d7ce 100644 --- a/tools/libs/light/Makefile +++ b/tools/libs/light/Makefile @@ -76,6 +76,7 @@ SRCS-y +=3D libxl.c SRCS-y +=3D libxl_create.c SRCS-y +=3D libxl_dm.c SRCS-y +=3D libxl_pci.c +SRCS-y +=3D libxl_pcid.c SRCS-y +=3D libxl_vchan.c SRCS-y +=3D libxl_dom.c SRCS-y +=3D libxl_exec.c diff --git a/tools/libs/light/libxl_pcid.c b/tools/libs/light/libxl_pcid.c new file mode 100644 index 0000000000..6317c77a3c --- /dev/null +++ b/tools/libs/light/libxl_pcid.c @@ -0,0 +1,134 @@ +/* + 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 + +#define DOM0_ID 0 + +static int pcid_handle_message(libxl__gc *gc, const libxl__json_object *re= quest, + libxl__json_object **result) +{ + const libxl__json_object *command_obj; + char *command_name; + + command_obj =3D libxl__json_map_get(VCHAN_MSG_EXECUTE, request, JSON_A= NY); + if (!command_obj) { + LOGE(ERROR, "Execution command not found\n"); + return ERROR_FAIL; + } + command_name =3D command_obj->u.string; + + return 0; +} + +static char *pcid_prepare_reply(libxl__gc *gc, const char *cmd, + libxl__json_object *result, int id) +{ + yajl_gen hand =3D NULL; + /* memory for 'buf' is owned by 'hand' */ + const unsigned char *buf; + libxl_yajl_length len; + yajl_gen_status s; + char *ret =3D NULL; + int rc; + + hand =3D libxl_yajl_gen_alloc(NULL); + if (!hand) { + LOGE(ERROR, "Error with hand allocation\n"); + goto out; + } + +#if HAVE_YAJL_V2 + /* Disable beautify for data */ + yajl_gen_config(hand, yajl_gen_beautify, 0); +#endif + + yajl_gen_map_open(hand); + if ( !result ) + libxl__yajl_gen_asciiz(hand, VCHAN_MSG_ERROR); + else { + libxl__yajl_gen_asciiz(hand, VCHAN_MSG_RETURN); + rc =3D libxl__json_object_to_yajl_gen(gc, hand, result); + if (rc) + goto get_buf_fail; + } + libxl__yajl_gen_asciiz(hand, PCID_MSG_FIELD_ID); + yajl_gen_integer(hand, id); + yajl_gen_map_close(hand); + + s =3D yajl_gen_get_buf(hand, &buf, &len); + if (s !=3D yajl_gen_status_ok) { + goto get_buf_fail; + } + + ret =3D libxl__sprintf(gc, "%*.*s" END_OF_MESSAGE, (int)len, (int)len,= buf); + +get_buf_fail: + yajl_gen_free(hand); +out: + + return ret; +} + +int libxl_pcid_process(libxl_ctx *ctx) +{ + GC_INIT(ctx); + struct vchan_info *vchan; + char *xs_path; + int ret =3D 0; + + vchan =3D libxl__zalloc(gc, sizeof(*vchan)); + xs_path =3D GCSPRINTF(PCID_XS_DIR"%d"PCID_XS_PATH, DOM0_ID); + + vchan->state =3D vchan_get_instance(gc, DOM0_ID, xs_path, VCHAN_SERVER= ); + if (!(vchan->state)) { + ret =3D -1; + goto out; + } + + vchan->handle_msg =3D pcid_handle_message; + vchan->prepare_cmd =3D pcid_prepare_reply; + vchan->receive_buf_size =3D PCI_RECEIVE_BUFFER_SIZE; + vchan->max_buf_size =3D PCI_MAX_SIZE_RX_BUF; + + while (true) { + ret =3D vchan_process_command(gc, vchan); + if (ret < 0) + break; + } + +out: + GC_FREE; + return ret; +} diff --git a/tools/libs/light/libxl_types.idl b/tools/libs/light/libxl_type= s.idl index 395165d801..d89c3ac1eb 100644 --- a/tools/libs/light/libxl_types.idl +++ b/tools/libs/light/libxl_types.idl @@ -76,6 +76,7 @@ libxl_error =3D Enumeration("error", [ (-30, "QMP_DEVICE_NOT_ACTIVE"), # a device has failed to be become act= ive (-31, "QMP_DEVICE_NOT_FOUND"), # the requested device has not been fou= nd (-32, "QEMU_API"), # QEMU's replies don't contains expected members + (-33, "PROTOCOL_ERROR_PCID"), ], value_namespace =3D "") =20 libxl_domain_type =3D Enumeration("domain_type", [ diff --git a/tools/libs/light/libxl_types_internal.idl b/tools/libs/light/l= ibxl_types_internal.idl index d66da13764..b9cee64d41 100644 --- a/tools/libs/light/libxl_types_internal.idl +++ b/tools/libs/light/libxl_types_internal.idl @@ -58,3 +58,9 @@ libxl__device_action =3D Enumeration("device_action", [ (1, "ADD"), (2, "REMOVE"), ]) + +libxl__pcid_message_type =3D Enumeration("pcid_message_type", [ + (1, "return"), + (2, "error"), + (3, "invalid"), + ]) --=20 2.17.1 From nobody Thu May 2 11:40:14 2024 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=1628778196; cv=none; d=zohomail.com; s=zohoarc; b=d2nYkS2yP5t4bbWEsQISFy3VajEdviFtq9WwX81bGlcsoyjr+0OQemrNfz3NX1dmEeb3UaPYVk2H5SObpDJdyS/OYrvWy+zZMJKk8aRIusqyytg4AF3N5h4DeYL/cxOoca+Z3laCLJS0nImFvMZwG0DGQxP6mE0FlCpkLz3aUng= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628778196; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=Srj9DO7HVVk4SzWRcp//DEXK7xs3aikVhiiKYWb+whk=; b=IzWLokyaP0PKRNCYWk/Ni5Int0Y7sPPRkDX2x9+D/Qhch1KPGZ/CIqJI97N/qNUWx2EFmlR5RBU0iDxxdGcm3vI3Jycth0avCiSKi3KvyTEvsq7SRUznFSTAFnYaBF8J8LIABlAQQUiEk2btg2S9eKAnwkKpeAIVYphoS7laJKk= 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 1628778196937565.8659762002135; Thu, 12 Aug 2021 07:23:16 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.166460.303833 (Exim 4.92) (envelope-from ) id 1mEBbl-0003sQ-Tw; Thu, 12 Aug 2021 14:22:57 +0000 Received: by outflank-mailman (output) from mailman id 166460.303833; Thu, 12 Aug 2021 14:22:57 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mEBbl-0003sE-O1; Thu, 12 Aug 2021 14:22:57 +0000 Received: by outflank-mailman (input) for mailman id 166460; Thu, 12 Aug 2021 14:22:56 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mEBbj-0002Q2-VE for xen-devel@lists.xenproject.org; Thu, 12 Aug 2021 14:22:56 +0000 Received: from mail-lf1-x130.google.com (unknown [2a00:1450:4864:20::130]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id c5532888-2723-4c75-ab13-552b949f56b7; Thu, 12 Aug 2021 14:22:38 +0000 (UTC) Received: by mail-lf1-x130.google.com with SMTP id g13so13686930lfj.12 for ; Thu, 12 Aug 2021 07:22:38 -0700 (PDT) Received: from localhost.localdomain (host-176-36-213-80.b024.la.net.ua. [176.36.213.80]) by smtp.gmail.com with ESMTPSA id s16sm329678ljj.48.2021.08.12.07.22.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Aug 2021 07:22:35 -0700 (PDT) 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: c5532888-2723-4c75-ab13-552b949f56b7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Srj9DO7HVVk4SzWRcp//DEXK7xs3aikVhiiKYWb+whk=; b=rgLgCupz5GKxOgQiBQHystEjZ8v7jBsCfY5WsVatRepq2ou5Q6crGH1bGccnwCOLv7 o7+fg/VazGEO2bJdDDlVYc48eFcFhhvSU3Wx5QHSJXp45RLLN6UXm2tolRSrRhTXC4Nw DaENImdI1zuEc9dLeO72Vr8Jz1c6beLbawCjJ0m59fqh+PJ1sdzUD6qtNrAcJyVg0cIr KK1EuBWk8XAHYI00Fs0mOWjSPh0YndNGeof0yaONGKqELy19t78p5LZWhhrflpl0+Tv6 2fqyMFhr+9FapyEHnHSQiunEgiTKr+p3gEUJqPWGeKturu0um8/bI+XINSE6VY0sFTb3 tuKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Srj9DO7HVVk4SzWRcp//DEXK7xs3aikVhiiKYWb+whk=; b=GDMkdMqTxwiVnYn+vZIYEhKUfFQjrT695gCHqHk7EuoavZQDabj/pRTr244PVrrs2t UzmtMxInOi+M6ifi1usqrW5CPM/y9I+T/F989t0Euwo8q5jMJX0Q92a1NvrT3IZAYNf1 +LLwxHDousSPeKBlFH8IcOP+z/PbDufgxOvfOlaGOqaE5odyYEvmJ9px/ap3vMjkDImP ZNtfLLog/RHtwLroZyMk6fDyTQQ+JSCcOnp/99BvQAKOQyzU7DA+jE9nSDAmCVoUGMDO EA4bRqK6VAW6qOJ38H92/eDF9FA8OiaRfkfAzXVYH//yhGIj6x4/5L2FPleo+d0c0RT9 uOoQ== X-Gm-Message-State: AOAM533/wu4jFQo1lt/S1t7ThEEAsQ3TV6ejPmKOVM0eXi8s4kAvCTFh IwJrZ7xgPrBBmJJlE0NliEQZH12AIosMew== X-Google-Smtp-Source: ABdhPJzIDmPpI2NV1Z+nnvGRy+mYxm9LgTsqof4DzsgEkpP6Lpe6fOLwjWLgBlSWzrZfZ7P5DkTTyA== X-Received: by 2002:a05:6512:6b:: with SMTP id i11mr2602548lfo.499.1628778155747; Thu, 12 Aug 2021 07:22:35 -0700 (PDT) From: Anastasiia Lukianenko To: xen-devel@lists.xenproject.org Cc: oleksandr_tyshchenko@epam.com, artem_mygaiev@epam.com, rahul.singh@arm.com, bertrand.marquis@arm.com, oleksandr_andrushchenko@epam.com, roger.pau@citrix.com, julien@xen.org, sstabellini@kernel.org, andr2000@gmail.com, vicooodin@gmail.com, Anastasiia Lukianenko Subject: [PATCH 3/9] tools/xl: Add pcid daemon to xl Date: Thu, 12 Aug 2021 17:22:24 +0300 Message-Id: <20210812142230.19353-4-vicooodin@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210812142230.19353-1-vicooodin@gmail.com> References: <20210812142230.19353-1-vicooodin@gmail.com> X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628778198585100003 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Anastasiia Lukianenko Add draft version of pcid server (based on vchan-node2), which can receive messages from the client. Signed-off-by: Anastasiia Lukianenko --- tools/configure | 5 +- tools/hotplug/FreeBSD/rc.d/xlpcid.in | 75 ++++++++++++++++++++++++++ tools/hotplug/Linux/init.d/xlpcid.in | 76 ++++++++++++++++++++++++++ tools/hotplug/NetBSD/rc.d/xlpcid.in | 75 ++++++++++++++++++++++++++ tools/xl/Makefile | 4 +- tools/xl/xl.h | 1 + tools/xl/xl_cmdtable.c | 7 +++ tools/xl/xl_pcid.c | 79 ++++++++++++++++++++++++++++ 8 files changed, 319 insertions(+), 3 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/NetBSD/rc.d/xlpcid.in create mode 100644 tools/xl/xl_pcid.c diff --git a/tools/configure b/tools/configure index b21ade08c0..be9fc9b1d4 100755 --- a/tools/configure +++ b/tools/configure @@ -2454,7 +2454,7 @@ ac_compiler_gnu=3D$ac_cv_c_compiler_gnu =20 =20 =20 -ac_config_files=3D"$ac_config_files ../config/Tools.mk hotplug/FreeBSD/rc.= d/xencommons hotplug/FreeBSD/rc.d/xendriverdomain hotplug/Linux/init.d/sysc= onfig.xencommons hotplug/Linux/init.d/sysconfig.xendomains hotplug/Linux/in= it.d/xen-watchdog hotplug/Linux/init.d/xencommons hotplug/Linux/init.d/xend= omains hotplug/Linux/init.d/xendriverdomain hotplug/Linux/launch-xenstore h= otplug/Linux/vif-setup hotplug/Linux/xen-hotplug-common.sh hotplug/Linux/xe= ndomains hotplug/NetBSD/rc.d/xencommons hotplug/NetBSD/rc.d/xendriverdomain= ocaml/xenstored/oxenstored.conf" +ac_config_files=3D"$ac_config_files ../config/Tools.mk hotplug/FreeBSD/rc.= d/xencommons hotplug/FreeBSD/rc.d/xendriverdomain hotplug/FreeBSD/rc.d/xlpc= id hotplug/Linux/init.d/sysconfig.xencommons hotplug/Linux/init.d/sysconfig= .xendomains hotplug/Linux/init.d/xlpcid hotplug/Linux/init.d/xen-watchdog h= otplug/Linux/init.d/xencommons hotplug/Linux/init.d/xendomains hotplug/Linu= x/init.d/xendriverdomain hotplug/Linux/launch-xenstore hotplug/Linux/vif-se= tup hotplug/Linux/xen-hotplug-common.sh hotplug/Linux/xendomains hotplug/Ne= tBSD/rc.d/xencommons hotplug/NetBSD/rc.d/xendriverdomain hotplug/NetBSD/rc.= d/xlpcid ocaml/xenstored/oxenstored.conf" =20 ac_config_headers=3D"$ac_config_headers config.h" =20 @@ -10892,8 +10892,10 @@ do "../config/Tools.mk") CONFIG_FILES=3D"$CONFIG_FILES ../config/Tools.mk= " ;; "hotplug/FreeBSD/rc.d/xencommons") CONFIG_FILES=3D"$CONFIG_FILES hotpl= ug/FreeBSD/rc.d/xencommons" ;; "hotplug/FreeBSD/rc.d/xendriverdomain") CONFIG_FILES=3D"$CONFIG_FILES = hotplug/FreeBSD/rc.d/xendriverdomain" ;; + "hotplug/FreeBSD/rc.d/xlpcid") CONFIG_FILES=3D"$CONFIG_FILES hotplug/F= reeBSD/rc.d/xlpcid" ;; "hotplug/Linux/init.d/sysconfig.xencommons") CONFIG_FILES=3D"$CONFIG_F= ILES hotplug/Linux/init.d/sysconfig.xencommons" ;; "hotplug/Linux/init.d/sysconfig.xendomains") CONFIG_FILES=3D"$CONFIG_F= ILES hotplug/Linux/init.d/sysconfig.xendomains" ;; + "hotplug/Linux/init.d/xlpcid") CONFIG_FILES=3D"$CONFIG_FILES hotplug/L= inux/init.d/xlpcid" ;; "hotplug/Linux/init.d/xen-watchdog") CONFIG_FILES=3D"$CONFIG_FILES hot= plug/Linux/init.d/xen-watchdog" ;; "hotplug/Linux/init.d/xencommons") CONFIG_FILES=3D"$CONFIG_FILES hotpl= ug/Linux/init.d/xencommons" ;; "hotplug/Linux/init.d/xendomains") CONFIG_FILES=3D"$CONFIG_FILES hotpl= ug/Linux/init.d/xendomains" ;; @@ -10904,6 +10906,7 @@ do "hotplug/Linux/xendomains") CONFIG_FILES=3D"$CONFIG_FILES hotplug/Linu= x/xendomains" ;; "hotplug/NetBSD/rc.d/xencommons") CONFIG_FILES=3D"$CONFIG_FILES hotplu= g/NetBSD/rc.d/xencommons" ;; "hotplug/NetBSD/rc.d/xendriverdomain") CONFIG_FILES=3D"$CONFIG_FILES h= otplug/NetBSD/rc.d/xendriverdomain" ;; + "hotplug/NetBSD/rc.d/xlpcid") CONFIG_FILES=3D"$CONFIG_FILES hotplug/Ne= tBSD/rc.d/xlpcid" ;; "ocaml/xenstored/oxenstored.conf") CONFIG_FILES=3D"$CONFIG_FILES ocaml= /xenstored/oxenstored.conf" ;; "config.h") CONFIG_HEADERS=3D"$CONFIG_HEADERS config.h" ;; "hotplug/Linux/systemd/proc-xen.mount") CONFIG_FILES=3D"$CONFIG_FILES = hotplug/Linux/systemd/proc-xen.mount" ;; diff --git a/tools/hotplug/FreeBSD/rc.d/xlpcid.in b/tools/hotplug/FreeBSD/r= c.d/xlpcid.in new file mode 100644 index 0000000000..2817bfaeed --- /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=3D@CONFIG_DIR@/@CONFIG_LEAF_DIR@ + +test -f $xencommons_config/xencommons && . $xencommons_config/xencommons + +XLPCID_PIDFILE=3D"@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 xl pcid... + ${sbindir}/xl pcid --pidfile=3D$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/ini= t.d/xlpcid.in new file mode 100644 index 0000000000..dce660098c --- /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=3D@CONFIG_DIR@/@CONFIG_LEAF_DIR@ + +test -f $xencommons_config/xencommons && . $xencommons_config/xencommons + +XLPCID_PIDFILE=3D"@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 xl pcid... + ${sbindir}/xl pcid --pidfile=3D$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/NetBSD/rc.d/xlpcid.in b/tools/hotplug/NetBSD/rc.= d/xlpcid.in new file mode 100644 index 0000000000..2817bfaeed --- /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=3D@CONFIG_DIR@/@CONFIG_LEAF_DIR@ + +test -f $xencommons_config/xencommons && . $xencommons_config/xencommons + +XLPCID_PIDFILE=3D"@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 xl pcid... + ${sbindir}/xl pcid --pidfile=3D$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/xl/Makefile b/tools/xl/Makefile index d35570c343..d46a9ad24a 100644 --- a/tools/xl/Makefile +++ b/tools/xl/Makefile @@ -22,7 +22,7 @@ XL_OBJS =3D xl.o xl_cmdtable.o xl_sxp.o xl_utils.o $(XL_O= BJS-y) XL_OBJS +=3D xl_parse.o xl_cpupool.o xl_flask.o XL_OBJS +=3D xl_vtpm.o xl_block.o xl_nic.o xl_usb.o XL_OBJS +=3D xl_sched.o xl_pci.o xl_vcpu.o xl_cdrom.o xl_mem.o -XL_OBJS +=3D xl_info.o xl_console.o xl_misc.o +XL_OBJS +=3D xl_info.o xl_console.o xl_misc.o xl_pcid.o XL_OBJS +=3D xl_vmcontrol.o xl_saverestore.o xl_migrate.o XL_OBJS +=3D xl_vdispl.o xl_vsnd.o xl_vkb.o xl_vcamera.o =20 @@ -39,7 +39,7 @@ $(XL_OBJS): _paths.h all: xl =20 xl: $(XL_OBJS) - $(CC) $(LDFLAGS) -o $@ $(XL_OBJS) $(LDLIBS_libxenutil) $(LDLIBS_libxenlig= ht) $(LDLIBS_libxentoollog) $(LDLIBS_libxenvchan) -lyajl $(APPEND_LDFLAGS) + $(CC) $(LDFLAGS) -o $@ $(XL_OBJS) $(LDLIBS_libxenstore) $(LDLIBS_libxenut= il) $(LDLIBS_libxenlight) $(LDLIBS_libxentoollog) $(LDLIBS_libxenvchan) -ly= ajl $(APPEND_LDFLAGS) =20 .PHONY: install install: all diff --git a/tools/xl/xl.h b/tools/xl/xl.h index e1d5190cc8..a88f03c624 100644 --- a/tools/xl/xl.h +++ b/tools/xl/xl.h @@ -206,6 +206,7 @@ int main_loadpolicy(int argc, char **argv); int main_remus(int argc, char **argv); #endif int main_devd(int argc, char **argv); +int main_pcid(int argc, char **argv); #if defined(__i386__) || defined(__x86_64__) int main_psr_hwinfo(int argc, char **argv); int main_psr_cmt_attach(int argc, char **argv); diff --git a/tools/xl/xl_cmdtable.c b/tools/xl/xl_cmdtable.c index c3cb515348..dfc4386b35 100644 --- a/tools/xl/xl_cmdtable.c +++ b/tools/xl/xl_cmdtable.c @@ -560,6 +560,13 @@ const struct cmd_spec cmd_table[] =3D { "-F Run in the foreground.\n" "-p, --pidfile [FILE] Write PID to pidfile when daemonizing.", }, + { "pcid", + &main_pcid, 0, 1, + "Daemon that acts as a server for the client in the libxl PCI", + "[options]", + "-F Run in the foreground.\n" + "-p, --pidfile [FILE] Write PID to pidfile when daemonizing.", + }, #if defined(__i386__) || defined(__x86_64__) { "psr-hwinfo", &main_psr_hwinfo, 0, 1, diff --git a/tools/xl/xl_pcid.c b/tools/xl/xl_pcid.c new file mode 100644 index 0000000000..f0635579ca --- /dev/null +++ b/tools/xl/xl_pcid.c @@ -0,0 +1,79 @@ +/* + 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 "xl.h" +#include "xl_utils.h" +#include "xl_parse.h" + +#include +#include +#include +#include +#include +#include + +#include +#include + +/* + * TODO: Running this code in multi-threaded environment + * Now the code is designed so that only one request to the server + * from the client is made in one domain. In the future, it is necessary + * to take into account cases when from different domains there can be + * several requests from a client at the same time. Therefore, it will be + * necessary to regulate the multithreading of processes for global variab= les. + */ + +int main_pcid(int argc, char *argv[]) +{ + int opt =3D 0, daemonize =3D 1, ret; + const char *pidfile =3D NULL; + static const struct option opts[] =3D { + {"pidfile", 1, 0, 'p'}, + COMMON_LONG_OPTS, + {0, 0, 0, 0} + }; + + SWITCH_FOREACH_OPT(opt, "Fp:", opts, "pcid", 0) { + case 'F': + daemonize =3D 0; + break; + case 'p': + pidfile =3D optarg; + break; + } + + if (daemonize) { + ret =3D do_daemonize("xlpcid", pidfile); + if (ret) { + ret =3D (ret =3D=3D 1) ? 0 : ret; + goto out_daemon; + } + } + + libxl_pcid_process(ctx); + +out_daemon: + exit(1); +} --=20 2.17.1 From nobody Thu May 2 11:40:14 2024 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=1628778187; cv=none; d=zohomail.com; s=zohoarc; b=LVKhkSc3Ri1VST37RGIpLm7+F/StUazI8jeqtT3XqQgw74TLAM3No30AhlMSOmAjhrMLcCIHkyAQQTZijkL5yuU2s0/c6bIH57uL4FhhcQLNswzKwjSgo7Mi6Y1TBibiY60y51ovNJg3kL/y+v/cyt3+yftkYAKgVWz2FtRuGK8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628778187; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=ffOhNeCRLI4R9q5Bex6AKuS0TCu8t1Ca81Imzl0FQU4=; b=jRhWTzvqaMvCDd6fzAN0PsDfdd1Ck5KawzfuovVmGd5wZLQP6gHSxMt7GZDzCRU3FNhGiUjacZTSEXH+FV1ybVDGlw7XLpdk6w832DvnDxDQeI89G8uHQ91K+XD5nIKndTjlv1be5pIbvzcoZwy/2VmgUE4Sae36aksofXkNTwE= 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 1628778187570869.8544995465203; Thu, 12 Aug 2021 07:23:07 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.166459.303822 (Exim 4.92) (envelope-from ) id 1mEBbg-0003PR-Jj; Thu, 12 Aug 2021 14:22:52 +0000 Received: by outflank-mailman (output) from mailman id 166459.303822; Thu, 12 Aug 2021 14:22:52 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mEBbg-0003PK-Eq; Thu, 12 Aug 2021 14:22:52 +0000 Received: by outflank-mailman (input) for mailman id 166459; Thu, 12 Aug 2021 14:22:51 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mEBbe-0002Q2-V5 for xen-devel@lists.xenproject.org; Thu, 12 Aug 2021 14:22:50 +0000 Received: from mail-lj1-x231.google.com (unknown [2a00:1450:4864:20::231]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 5bfdb2b2-699a-4f18-8ccb-a597d9fd501f; Thu, 12 Aug 2021 14:22:38 +0000 (UTC) Received: by mail-lj1-x231.google.com with SMTP id m17so7048914ljp.7 for ; Thu, 12 Aug 2021 07:22:38 -0700 (PDT) Received: from localhost.localdomain (host-176-36-213-80.b024.la.net.ua. [176.36.213.80]) by smtp.gmail.com with ESMTPSA id s16sm329678ljj.48.2021.08.12.07.22.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Aug 2021 07:22:36 -0700 (PDT) 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: 5bfdb2b2-699a-4f18-8ccb-a597d9fd501f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ffOhNeCRLI4R9q5Bex6AKuS0TCu8t1Ca81Imzl0FQU4=; b=Gidp/hw+hCR9SMN+KU8OZJAxlHMv/Gq6dX5jJOR3MhqwwLUNH/pwgHAyAiE+JrROpH 7HgWPDPoB7dPv1R+HLSnuZIlowvJBOjHq7ki/+dQ6XLxWyGsJH+PhXyOEVryp88Pd+TU FJo/T1kZVT88jFeQbsHLHoGMSvIlUp+FslrM9eWFExYhUmD4SI4i1GV/ioU4HYpzC4kk oXRGdDtXvkPliFKHdfp97W+m3kaCaFqjX5mOapXce37aVOh4PaqtyRlnLD7uEquFOpc+ Z9H/PTVIsC2xDUfhOjuxv3l8t7XWsKOcvuOi869gEQXr7gqDLhUW2fpq7ifcdQe+ui4x hOMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ffOhNeCRLI4R9q5Bex6AKuS0TCu8t1Ca81Imzl0FQU4=; b=MqX8W8YMCsZnve+SmqJ053IrQhFd0IkfYpxm7SVLXMjPAUyAm6vjBOrhsYlRbq7XwA PJosD0XRRI1m90lYb2hkSVnWtFYUcgA8zzkJ92AGQ0fK0/XD3W0W8ZzjYduOKMw8PH2t G0ss0AX+d0ihVNIvjEYtug0M3V+nweyiiz4Ms3jmAZgOHU2W5m0emhDwMV8wSHP54x4+ T+9c0JCHT3PxeS/4YZDZIPkgulXt5f4ksRoGvqtzAFSjoB3naR1rMQUEj8OlOFKmmbVc fD+3kCNr0fsP6HQbG4gfhKvDLS8wMNM2X466AXZlmsz1b7lv8ctjHp0d0R4wJWDhKVAI 7fQw== X-Gm-Message-State: AOAM530me4baAt5kEpRoixtSe/G0A2McxRHq/jPo2jXu+vV4QdptHNzS 30QT9/asSfoEtHUQu0oPCc3eMQZy+poE3Q== X-Google-Smtp-Source: ABdhPJw+s2eXh1HJSYkLLdDN0DRHU9Z9wDzvN5DARCbRXs6Jpf6MaUB0Rkse7zLIw2qsL99LEbUU3w== X-Received: by 2002:a05:651c:20f:: with SMTP id y15mr3054743ljn.137.1628778156977; Thu, 12 Aug 2021 07:22:36 -0700 (PDT) From: Anastasiia Lukianenko To: xen-devel@lists.xenproject.org Cc: oleksandr_tyshchenko@epam.com, artem_mygaiev@epam.com, rahul.singh@arm.com, bertrand.marquis@arm.com, oleksandr_andrushchenko@epam.com, roger.pau@citrix.com, julien@xen.org, sstabellini@kernel.org, andr2000@gmail.com, vicooodin@gmail.com, Anastasiia Lukianenko Subject: [PATCH 4/9] tools/libs/light: Add "ls" command processing to xl pcid daemon and libxl PCI Date: Thu, 12 Aug 2021 17:22:25 +0300 Message-Id: <20210812142230.19353-5-vicooodin@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210812142230.19353-1-vicooodin@gmail.com> References: <20210812142230.19353-1-vicooodin@gmail.com> X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628778189338100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Anastasiia Lukianenko Messaging format is based on json style. Xl pcid receives "ls" command from client and sends a reply: 1. success case - array of directory's names 2. error case - message with "error" in return Xl pcid server transmits information about devices assigned to the PCI driver by sending reply with directory's names. Libxl PCI processes reply message instead of opening directory in libxl side. Signed-off-by: Anastasiia Lukianenko --- tools/include/pcid.h | 7 ++ tools/libs/light/libxl_pci.c | 136 +++++++++++++++++++++++++++++----- tools/libs/light/libxl_pcid.c | 59 +++++++++++++++ 3 files changed, 184 insertions(+), 18 deletions(-) diff --git a/tools/include/pcid.h b/tools/include/pcid.h index 59a7aad64a..e9c3c497c6 100644 --- a/tools/include/pcid.h +++ b/tools/include/pcid.h @@ -28,12 +28,19 @@ #define PCID_MSG_FIELD_ID "id" #define PCID_MSG_FIELD_ARGS "arguments" =20 +#define PCID_CMD_LIST "ls" +#define PCID_CMD_DIR_ID "dir_id" + #define PCID_PCIBACK_DRIVER "pciback_driver" =20 #if defined(__linux__) #define SYSFS_PCIBACK_DRIVER "/sys/bus/pci/drivers/pciback" #endif =20 +#define PCI_INFO_PATH "/libxl/pci" +#define PCI_BDF_XSPATH "%04x-%02x-%02x-%01x" +#define PCI_BDF "%04x:%02x:%02x.%01x" + int libxl_pcid_process(libxl_ctx *ctx); =20 #endif /* PCID_H */ diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c index 30e203c264..6534b70777 100644 --- a/tools/libs/light/libxl_pci.c +++ b/tools/libs/light/libxl_pci.c @@ -17,6 +17,9 @@ #include "libxl_osdeps.h" /* must come before any other headers */ =20 #include "libxl_internal.h" +#include "libxl_vchan.h" + +#include =20 #define PCI_BDF "%04x:%02x:%02x.%01x" #define PCI_BDF_SHORT "%02x:%02x.%01x" @@ -47,6 +50,100 @@ static void pci_struct_fill(libxl_device_pci *pci, unsi= gned int domain, pci->func =3D func; } =20 +static libxl__pcid_message_type pci_response_type(const libxl__json_object= *o) +{ + libxl__pcid_message_type type; + libxl__json_map_node *node =3D NULL; + int i; + + for (i =3D 0; (node =3D libxl__json_map_node_get(o, i)); i++) { + if (libxl__pcid_message_type_from_string(node->map_key, &type) =3D= =3D 0) + return type; + } + return LIBXL__PCID_MESSAGE_TYPE_INVALID; +} + +static int pci_handle_msg(libxl__gc *gc, const libxl__json_object *request, + libxl__json_object **result) +{ + libxl__pcid_message_type type =3D pci_response_type(request); + + if (type =3D=3D LIBXL__PCID_MESSAGE_TYPE_RETURN) + *result =3D (libxl__json_object *)libxl__json_map_get(VCHAN_MSG_RE= TURN, + request, JSON_= ANY); + + return 0; +} + +static char *pci_prepare_cmd(libxl__gc *gc, const char *cmd, + libxl__json_object *args, int id) +{ + yajl_gen hand =3D NULL; + /* memory for 'buf' is owned by 'hand' */ + const unsigned char *buf; + libxl_yajl_length len; + yajl_gen_status s; + char *ret =3D NULL; + + hand =3D libxl_yajl_gen_alloc(NULL); + + if (!hand) + return NULL; + +#if HAVE_YAJL_V2 + /* Disable beautify for data */ + yajl_gen_config(hand, yajl_gen_beautify, 0); +#endif + + yajl_gen_map_open(hand); + libxl__yajl_gen_asciiz(hand, VCHAN_MSG_EXECUTE); + libxl__yajl_gen_asciiz(hand, cmd); + libxl__yajl_gen_asciiz(hand, PCID_MSG_FIELD_ID); + yajl_gen_integer(hand, id); + if (args) { + libxl__yajl_gen_asciiz(hand, PCID_MSG_FIELD_ARGS); + libxl__json_object_to_yajl_gen(gc, hand, args); + } + yajl_gen_map_close(hand); + + s =3D yajl_gen_get_buf(hand, &buf, &len); + + if (s !=3D yajl_gen_status_ok) + goto out; + + ret =3D libxl__sprintf(gc, "%*.*s" END_OF_MESSAGE, + (int)len, (int)len, buf); + +out: + yajl_gen_free(hand); + return ret; +} + +static struct vchan_info *pci_prepare_vchan(libxl__gc *gc) +{ + struct vchan_info *vchan; + libxl_domid domid; + char *xs_path; + + domid =3D vchan_find_server(gc, PCID_XS_DIR, PCID_XS_PATH); + if (domid =3D=3D DOMID_INVALID) { + LOGE(ERROR, "Can't find vchan server"); + return NULL; + } + vchan =3D libxl__zalloc(gc, sizeof(*vchan)); + xs_path =3D GCSPRINTF(PCID_XS_DIR"%d"PCID_XS_PATH, domid); + vchan->state =3D vchan_get_instance(gc, domid, xs_path, VCHAN_CLIENT); + if (!(vchan->state)) + return NULL; + + vchan->handle_msg =3D pci_handle_msg; + vchan->prepare_cmd =3D pci_prepare_cmd; + vchan->receive_buf_size =3D PCI_RECEIVE_BUFFER_SIZE; + vchan->max_buf_size =3D PCI_MAX_SIZE_RX_BUF; + + return vchan; +} + static void libxl_create_pci_backend_device(libxl__gc *gc, flexarray_t *back, int num, @@ -429,33 +526,37 @@ static void pci_info_xs_remove(libxl__gc *gc, libxl_d= evice_pci *pci, =20 libxl_device_pci *libxl_device_pci_assignable_list(libxl_ctx *ctx, int *nu= m) { - GC_INIT(ctx); libxl_device_pci *pcis =3D NULL, *new; - struct dirent *de; - DIR *dir; =20 + GC_INIT(ctx); *num =3D 0; + struct vchan_info *vchan; + libxl__json_object *args =3D NULL, *result =3D NULL, *dir; + int i; + const char *dir_name; =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_prepare_vchan(gc); + if (!vchan) goto out; - } =20 - while((de =3D readdir(dir))) { - unsigned int dom, bus, dev, func; - char *name; + libxl__vchan_param_add_string(gc, &args, PCID_CMD_DIR_ID, + PCID_PCIBACK_DRIVER); + result =3D vchan_send_command(gc, vchan, PCID_CMD_LIST, args); + if (!result) + goto out; =20 - if (sscanf(de->d_name, PCI_BDF, &dom, &bus, &dev, &func) !=3D 4) + for (i =3D 0; (dir =3D libxl__json_array_get(result, i)); i++) { + dir_name =3D libxl__json_object_get_string(dir); + unsigned dom, bus, dev, func; + char *name; + if (sscanf(dir_name, PCI_BDF, &dom, &bus, &dev, &func) !=3D 4) continue; =20 new =3D realloc(pcis, ((*num) + 1) * sizeof(*new)); - if (NULL =3D=3D new) - continue; + if (new =3D=3D NULL) { + LOGE(ERROR, "Couldn't realloc pcis struct for new entry"); + break; + } =20 pcis =3D new; new =3D pcis + *num; @@ -472,7 +573,6 @@ libxl_device_pci *libxl_device_pci_assignable_list(libx= l_ctx *ctx, int *num) (*num)++; } =20 - closedir(dir); out: GC_FREE; return pcis; diff --git a/tools/libs/light/libxl_pcid.c b/tools/libs/light/libxl_pcid.c index 6317c77a3c..c897244e8a 100644 --- a/tools/libs/light/libxl_pcid.c +++ b/tools/libs/light/libxl_pcid.c @@ -36,6 +36,57 @@ =20 #define DOM0_ID 0 =20 +static struct libxl__json_object *process_ls_cmd(libxl__gc *gc, + const struct libxl__json_= object *resp) +{ + libxl__json_object *result =3D NULL; + const libxl__json_object *args, *dir_id; + struct libxl__json_object *node; + char *dir_name; + struct dirent *de; + DIR *dir =3D NULL; + + args =3D libxl__json_map_get(PCID_MSG_FIELD_ARGS, resp, JSON_MAP); + if (!args) + goto out; + dir_id =3D libxl__json_map_get(PCID_CMD_DIR_ID, args, JSON_ANY); + if (!dir_id) + goto out; + + dir_name =3D dir_id->u.string; + + if (strcmp(PCID_PCIBACK_DRIVER, dir_name) =3D=3D 0) + dir =3D opendir(SYSFS_PCIBACK_DRIVER); + else { + LOGE(ERROR, "Unknown directory: %s\n", dir_name); + goto out; + } + + if (dir =3D=3D NULL) { + if (errno =3D=3D ENOENT) + LOGE(ERROR, "Looks like pciback driver not loaded\n"); + else + LOGE(ERROR, "Couldn't open %s\n", dir_name); + goto out; + } + + result =3D libxl__json_object_alloc(gc, JSON_ARRAY); + if (!result) { + LOGE(ERROR, "Memory allocation failed\n"); + goto out; + } + while ((de =3D readdir(dir))) { + node =3D libxl__json_object_alloc(gc, JSON_STRING); + node->u.string =3D de->d_name; + flexarray_append(result->u.array, node); + } + + closedir(dir); + +out: + return result; +} + static int pcid_handle_message(libxl__gc *gc, const libxl__json_object *re= quest, libxl__json_object **result) { @@ -49,6 +100,14 @@ static int pcid_handle_message(libxl__gc *gc, const lib= xl__json_object *request, } command_name =3D command_obj->u.string; =20 + if (strcmp(command_name, PCID_CMD_LIST) =3D=3D 0) + *result =3D process_ls_cmd(gc, request); + else + return ERROR_NOTFOUND; + + if (!result) + return ERROR_FAIL; + return 0; } =20 --=20 2.17.1 From nobody Thu May 2 11:40:14 2024 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=1628778195; cv=none; d=zohomail.com; s=zohoarc; b=FU9Kc/LjJyb86JVl8zl2jdIEQqiiUAkhzu9HgkeG7b3LK56c6346SD84+Buk5QMN3DtEIkWKUtt0KN2DapCaovvzPezX2bsfvpyRDg69ylBkznBbkiE57Se1fdP1Dm/Z8dVIXaZ11ci++HauCyqXfjKHOy5Iyie33waO+1KOmvU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628778195; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=lsigEyEQd5J6XuvBHqhWnDNkEtofXcrhnILksWH00iM=; b=kicsXXcWyBggby7qdbLPnJkiOpr8W7tsW1972G//jJH0KM1TKta8aa2wfcoTnMCVzrCncOYCy18rLJIp7lg/CkXikzSQoLe5tKD7PlY61InbnrknmeFL7uNzjpUAeH6BnbAQPlukVIQRviItx9hEIf4L/fsoyD4To3zIKgu1xgI= 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 162877819596360.085996585561816; Thu, 12 Aug 2021 07:23:15 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.166463.303844 (Exim 4.92) (envelope-from ) id 1mEBbq-0004SH-86; Thu, 12 Aug 2021 14:23:02 +0000 Received: by outflank-mailman (output) from mailman id 166463.303844; Thu, 12 Aug 2021 14:23:02 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mEBbq-0004Ru-2R; Thu, 12 Aug 2021 14:23:02 +0000 Received: by outflank-mailman (input) for mailman id 166463; Thu, 12 Aug 2021 14:23:01 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mEBbo-0002Q2-VG for xen-devel@lists.xenproject.org; Thu, 12 Aug 2021 14:23:00 +0000 Received: from mail-lj1-x232.google.com (unknown [2a00:1450:4864:20::232]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 8f146ec4-2442-4c88-866a-7dd44b380abc; Thu, 12 Aug 2021 14:22:39 +0000 (UTC) Received: by mail-lj1-x232.google.com with SMTP id n6so10773714ljp.9 for ; Thu, 12 Aug 2021 07:22:39 -0700 (PDT) Received: from localhost.localdomain (host-176-36-213-80.b024.la.net.ua. [176.36.213.80]) by smtp.gmail.com with ESMTPSA id s16sm329678ljj.48.2021.08.12.07.22.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Aug 2021 07:22:37 -0700 (PDT) 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: 8f146ec4-2442-4c88-866a-7dd44b380abc DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=lsigEyEQd5J6XuvBHqhWnDNkEtofXcrhnILksWH00iM=; b=XyW1gb9bH2YdxMPJ5EwY0SDVcFa6MyntkX+DUja59K3IcDwcEmCfkdsV32BYA+RPv4 bDDabzVqHhitdbLHZ0MR/dvQ4e9fwOO5xAyij8dPQRy8x/jV+W+ENMMwOxIzRkk5GPJM yGkphC405Ein9DH5GvASUlaXeqZOubYkohAdBVPXko53WimlfEVU9DNgB7z5lJOvdnsV UCELcjqjCpqIl1ueiPoUOT7fNQVJy5JMYZkkmUGC4SnYCxNJnmNTgrV8f1vSxeNSH1SO x0IWtSS4UytuDrA9t2U8vVkSKOMomFZg9D6uBh07IqhUu5M7ExpEhAdwInOu0NEbHCDz 2F3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=lsigEyEQd5J6XuvBHqhWnDNkEtofXcrhnILksWH00iM=; b=t1QGYLoLi3NONAZkESyqG9ZbYx1+sqWQwsXBzuYoJBhkwvjNznY/i/V7f4zULee0km k5KcYXgXiNaGhmBsFxMThpLEOhQlSFbBjHoQgJML27hkXh39NNHTA4tl0fmB79VDoWwq /i1BU1v5AIk+wtZgvU/S2jIpUhRYXvcy8CYQZEo2NddRdVlyVYi5QA56Iv+CwAidYaDN EWRchnTVz2Eqqi6JRnGPTxr2uGhraLjf6p74ywcnVgPwAzhcRh78njY7l0xX61WXAOJZ N3R7f5NksN6mmrGoaNenBzkEjpAXabPdQyalij8LvZ23bCRVBkpw27M+XSNzkoe7mWWn XmRw== X-Gm-Message-State: AOAM533vrZcMTNedPLaAjRX763ihZRg8pSd+ROr3uR6VOvKXbkb982XU Q8zDKE7QG1AI22HzNDzsmcqEnkTOvjY2dw== X-Google-Smtp-Source: ABdhPJxpz+xwdDf79Qy6LV8TTfBxB1VIPh8lU4qbF2yk0zFZkGDacHUowCUjhRFubiTJor/5+QTm4w== X-Received: by 2002:a2e:9a50:: with SMTP id k16mr3146463ljj.308.1628778158297; Thu, 12 Aug 2021 07:22:38 -0700 (PDT) From: Anastasiia Lukianenko To: xen-devel@lists.xenproject.org Cc: oleksandr_tyshchenko@epam.com, artem_mygaiev@epam.com, rahul.singh@arm.com, bertrand.marquis@arm.com, oleksandr_andrushchenko@epam.com, roger.pau@citrix.com, julien@xen.org, sstabellini@kernel.org, andr2000@gmail.com, vicooodin@gmail.com, Anastasiia Lukianenko Subject: [PATCH 5/9] tools/libs/light: Add "write" command to xl pcid and libxl PCI Date: Thu, 12 Aug 2021 17:22:26 +0300 Message-Id: <20210812142230.19353-6-vicooodin@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210812142230.19353-1-vicooodin@gmail.com> References: <20210812142230.19353-1-vicooodin@gmail.com> X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628778196596100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Anastasiia Lukianenko Add functions for handling "write" command on xl pcid side. Libxl PCI side receives reply: 1. success - string in "return" message 2. fail - error type of reply message Signed-off-by: Anastasiia Lukianenko --- tools/include/pcid.h | 7 ++++ tools/libs/light/libxl_pci.c | 69 +++++++++++++++++++---------------- tools/libs/light/libxl_pcid.c | 69 +++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 32 deletions(-) diff --git a/tools/include/pcid.h b/tools/include/pcid.h index e9c3c497c6..935d99b186 100644 --- a/tools/include/pcid.h +++ b/tools/include/pcid.h @@ -31,7 +31,14 @@ #define PCID_CMD_LIST "ls" #define PCID_CMD_DIR_ID "dir_id" =20 +#define PCID_CMD_WRITE "write" +#define PCID_CMD_PCI_PATH "pci_path" +#define PCID_CMD_PCI_INFO "pci_info" + #define PCID_PCIBACK_DRIVER "pciback_driver" +#define PCID_PCI_DEV "pci_dev" + +#define SYSFS_DRIVER_PATH "driver_path" =20 #if defined(__linux__) #define SYSFS_PCIBACK_DRIVER "/sys/bus/pci/drivers/pciback" diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c index 6534b70777..03ce42dec3 100644 --- a/tools/libs/light/libxl_pci.c +++ b/tools/libs/light/libxl_pci.c @@ -455,28 +455,34 @@ static bool is_pci_in_array(libxl_device_pci *pcis, i= nt num, } =20 /* Write the standard BDF into the sysfs path given by sysfs_path. */ -static int sysfs_write_bdf(libxl__gc *gc, const char * sysfs_path, +static int sysfs_write_bdf(libxl__gc *gc, const char *sysfs_path, + const char *pci_path, libxl_device_pci *pci) { - int rc, fd; char *buf; =20 - fd =3D open(sysfs_path, O_WRONLY); - if (fd < 0) { - LOGE(ERROR, "Couldn't open %s", sysfs_path); - return ERROR_FAIL; - } + struct vchan_info *vchan; + libxl__json_object *args =3D NULL, *result =3D NULL; =20 - buf =3D GCSPRINTF(PCI_BDF, pci->domain, pci->bus, - pci->dev, pci->func); - rc =3D 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); + vchan =3D pci_prepare_vchan(gc); + if (!vchan) + return ERROR_FAIL; =20 - if (rc < 0) + buf =3D GCSPRINTF(PCI_BDF, pci->domain, pci->bus, pci->dev, pci->func); + if (strcmp(SYSFS_PCI_DEV, sysfs_path) =3D=3D 0) + libxl__vchan_param_add_string(gc, &args, PCID_CMD_DIR_ID, PCID_PCI= _DEV); + else if (strcmp(SYSFS_PCIBACK_DRIVER, sysfs_path) =3D=3D 0) + libxl__vchan_param_add_string(gc, &args, PCID_CMD_DIR_ID, PCID_PCI= BACK_DRIVER); + else if (strcmp(SYSFS_DRIVER_PATH, sysfs_path) =3D=3D 0) + libxl__vchan_param_add_string(gc, &args, PCID_CMD_DIR_ID, SYSFS_DR= IVER_PATH); + + libxl__vchan_param_add_string(gc, &args, PCID_CMD_PCI_PATH, pci_path); + libxl__vchan_param_add_string(gc, &args, PCID_CMD_PCI_INFO, buf); + result =3D vchan_send_command(gc, vchan, PCID_CMD_WRITE, args); + if (!result) { + LOGE(WARN, "Write %s to %s failed\n", buf, sysfs_path); return ERROR_FAIL; + } =20 return 0; } @@ -593,14 +599,13 @@ void libxl_device_pci_assignable_list_free(libxl_devi= ce_pci *list, int num) static int sysfs_dev_unbind(libxl__gc *gc, libxl_device_pci *pci, char **driver_path) { - char * spath, *dp =3D NULL; + char *spath, *pci_path, *dp =3D NULL; struct stat st; =20 - spath =3D GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/driver", - pci->domain, - pci->bus, - pci->dev, - pci->func); + pci_path =3D GCSPRINTF("/"PCI_BDF"/driver", pci->domain, pci->bus, + pci->dev, pci->func); + + spath =3D GCSPRINTF(SYSFS_PCI_DEV"%s", pci_path); if ( !lstat(spath, &st) ) { /* Find the canonical path to the driver. */ dp =3D libxl__zalloc(gc, PATH_MAX); @@ -614,7 +619,7 @@ static int sysfs_dev_unbind(libxl__gc *gc, libxl_device= _pci *pci, =20 /* Unbind from the old driver */ spath =3D GCSPRINTF("%s/unbind", dp); - if ( sysfs_write_bdf(gc, spath, pci) < 0 ) { + if (sysfs_write_bdf(gc, SYSFS_PCI_DEV, pci_path, pci) < 0) { LOGE(ERROR, "Couldn't unbind device"); return -1; } @@ -816,14 +821,14 @@ static int pciback_dev_assign(libxl__gc *gc, libxl_de= vice_pci *pci) LOGE(ERROR, "Error checking for pciback slot"); return ERROR_FAIL; } else if (rc =3D=3D 0) { - if ( sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER"/new_slot", - pci) < 0 ) { + if (sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER, "/new_slot", + pci) < 0) { LOGE(ERROR, "Couldn't bind device to pciback!"); return ERROR_FAIL; } } =20 - if ( sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER"/bind", pci) < 0 ) { + if (sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER, "/bind", pci) < 0) { LOGE(ERROR, "Couldn't bind device to pciback!"); return ERROR_FAIL; } @@ -840,8 +845,8 @@ static int pciback_dev_unassign(libxl__gc *gc, libxl_de= vice_pci *pci) =20 /* Remove slot if necessary */ if ( pciback_dev_has_slot(gc, pci) > 0 ) { - if ( sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER"/remove_slot", - pci) < 0 ) { + if (sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER, "/remove_slot", + pci) < 0) { LOGE(ERROR, "Couldn't remove pciback slot"); return ERROR_FAIL; } @@ -1022,9 +1027,9 @@ static int libxl__device_pci_assignable_remove(libxl_= _gc *gc, if ( rebind ) { LOG(INFO, "Rebinding to driver at %s", driver_path); =20 - if ( sysfs_write_bdf(gc, - GCSPRINTF("%s/bind", driver_path), - pci) < 0 ) { + if (sysfs_write_bdf(gc, SYSFS_DRIVER_PATH, + GCSPRINTF("%s/bind", driver_path), + pci) < 0) { LOGE(ERROR, "Couldn't bind device to %s", driver_path); return -1; } @@ -1565,8 +1570,8 @@ static void pci_add_dm_done(libxl__egc *egc, =20 /* 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 (sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER, "/permissive", + pci) < 0) { LOGD(ERROR, domainid, "Setting permissive for device"); rc =3D ERROR_FAIL; goto out; diff --git a/tools/libs/light/libxl_pcid.c b/tools/libs/light/libxl_pcid.c index c897244e8a..ee4c832779 100644 --- a/tools/libs/light/libxl_pcid.c +++ b/tools/libs/light/libxl_pcid.c @@ -87,6 +87,73 @@ out: return result; } =20 +static int handle_write_cmd(libxl__gc *gc, char *sysfs_path, char *pci_inf= o) +{ + int rc, fd; + + fd =3D open(sysfs_path, O_WRONLY); + if (fd < 0) { + LOGE(ERROR, "Couldn't open %s\n", sysfs_path); + return ERROR_FAIL; + } + + rc =3D write(fd, pci_info, strlen(pci_info)); + close(fd); + if (rc < 0) { + LOGE(ERROR, "write to %s returned %d\n", sysfs_path, rc); + return ERROR_FAIL; + } + + return 0; +} + +static libxl__json_object *process_write_cmd(libxl__gc *gc, + const struct libxl__json_obje= ct *resp) +{ + libxl__json_object *result =3D NULL; + const struct libxl__json_object *args, *dir_id, *pci_path, *pci_info; + char *full_path; + int ret; + + args =3D libxl__json_map_get(PCID_MSG_FIELD_ARGS, resp, JSON_MAP); + if (!args) + goto out; + dir_id =3D libxl__json_map_get(PCID_CMD_DIR_ID, args, JSON_ANY); + if (!dir_id) + goto out; + pci_path =3D libxl__json_map_get(PCID_CMD_PCI_PATH, args, JSON_ANY); + if (!pci_path) + goto out; + pci_info =3D libxl__json_map_get(PCID_CMD_PCI_INFO, args, JSON_ANY); + if (!pci_info) + goto out; + + if (strcmp(dir_id->u.string, PCID_PCI_DEV) =3D=3D 0) + full_path =3D libxl__sprintf(gc, SYSFS_PCI_DEV"%s", pci_path->u.st= ring); + else if (strcmp(dir_id->u.string, PCID_PCIBACK_DRIVER) =3D=3D 0) + full_path =3D libxl__sprintf(gc, SYSFS_PCIBACK_DRIVER"%s", pci_pat= h->u.string); + else if (strcmp(dir_id->u.string, SYSFS_DRIVER_PATH) =3D=3D 0) + full_path =3D pci_path->u.string; + else { + LOGE(ERROR, "Unknown write directory %s\n", dir_id->u.string); + goto out; + } + + ret =3D handle_write_cmd(gc, full_path, pci_info->u.string); + if (ret !=3D 0) + goto out; + + result =3D libxl__json_object_alloc(gc, JSON_STRING); + if (!result) { + LOGE(ERROR, "Memory allocation failed\n"); + goto out; + } + result->u.string =3D pci_path->u.string; + +out: + return result; +} + static int pcid_handle_message(libxl__gc *gc, const libxl__json_object *re= quest, libxl__json_object **result) { @@ -102,6 +169,8 @@ static int pcid_handle_message(libxl__gc *gc, const lib= xl__json_object *request, =20 if (strcmp(command_name, PCID_CMD_LIST) =3D=3D 0) *result =3D process_ls_cmd(gc, request); + else if (strcmp(PCID_CMD_WRITE, command_name) =3D=3D 0) + *result =3D process_write_cmd(gc, request); else return ERROR_NOTFOUND; =20 --=20 2.17.1 From nobody Thu May 2 11:40:14 2024 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=1628778202; cv=none; d=zohomail.com; s=zohoarc; b=Kx4uesDERKSoXDsd85vUMiLR1NImOkUxyeB6stfjvYSdgSAHalycMNSWd2z0zQJuDpTwsHSG3x2jbtgAKsbG32lppUw8CL6a85TnF7ygntAJKP5vKo//CG5k9nNlTalTu77d8WVZ316mk5GpM2stQv7i+px7wQOMtnyOx4Juyek= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628778202; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=inCVhx2xTjXRTUWKhimZDtGVbzmLyu/AE4x7F5P7a2A=; b=T4ord8MjnFsvQgreURln1uG9ctMHsn8MhMyvJpKU/USDBkf1mW++C/sXspqS1TUlOVMosUe4J2IPRe1m4d09oE9sYbie0iVQnUnpqWIBdkxT8hDzE8fxHosTs31IuOXocSMlM33YEnmXJYehkCmzMQQArYR1esMWMlzU6WvXS5Y= 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 1628778202294583.3182128073092; Thu, 12 Aug 2021 07:23:22 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.166469.303855 (Exim 4.92) (envelope-from ) id 1mEBbv-00051K-MF; Thu, 12 Aug 2021 14:23:07 +0000 Received: by outflank-mailman (output) from mailman id 166469.303855; Thu, 12 Aug 2021 14:23:07 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mEBbv-00051D-IM; Thu, 12 Aug 2021 14:23:07 +0000 Received: by outflank-mailman (input) for mailman id 166469; Thu, 12 Aug 2021 14:23:06 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mEBbt-0002Q2-VL for xen-devel@lists.xenproject.org; Thu, 12 Aug 2021 14:23:05 +0000 Received: from mail-lj1-x22c.google.com (unknown [2a00:1450:4864:20::22c]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id cc5747a4-339f-47a8-9a74-7e8db8049f43; Thu, 12 Aug 2021 14:22:40 +0000 (UTC) Received: by mail-lj1-x22c.google.com with SMTP id n7so10906079ljq.0 for ; Thu, 12 Aug 2021 07:22:40 -0700 (PDT) Received: from localhost.localdomain (host-176-36-213-80.b024.la.net.ua. [176.36.213.80]) by smtp.gmail.com with ESMTPSA id s16sm329678ljj.48.2021.08.12.07.22.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Aug 2021 07:22:39 -0700 (PDT) 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: cc5747a4-339f-47a8-9a74-7e8db8049f43 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=inCVhx2xTjXRTUWKhimZDtGVbzmLyu/AE4x7F5P7a2A=; b=BKNQKgQfIFzmnp49yQKqVc+Wv10iSeHUsbMgMt+mD6Wu3tsk/Lm4F/t4nTzrnt7EEk J7VKbpCbwnPg+4Ai593QxG8urtUKJP8jyI5BcXXtH5vtmZro3JSwuWxieqQPHDenqwO5 i0YZlVICZE5C0zzRH2SDHqPbQGF/vDiZ1nR8/rdyTxHBRAmYf19r1yW0y+WqHk9vA+ci I+8VTApj1PGd4Hx1YuZmOWHtMN1xlse0LfZNGXRKbrjZ3y3o2S7tAgQxgOURM5dFEp58 +yNnjFkjQU21d73zNPWqI8gX9otcPRYXUNsxK3sBhhV4/Jyw6rUzI+EVK0Mgi82/aR8d Xd4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=inCVhx2xTjXRTUWKhimZDtGVbzmLyu/AE4x7F5P7a2A=; b=TPY6geC/dBkIfaa77bLplyftA+c9LfixGMauoU9nxQ6T7n3EU4PdRwrPs4b+HYxZuD lz/YrVjRxwCFAkbzd+nlOggFImVWbyJy4OHuoxLlAuyYZCQCDeHNjWpSIrlea+8qFHcs vh66tl3lQj27nDO2mhl8gxtOCd97nffOIxxRNUHC9VbsDb7uZFtWIAaczvI2dDmoK6eH 3+9Q1dWuj6aLd/NS9b711S3dHxrikuKDmJU4TIVJBRd2O/9JlBEtjv1yS3Tf5/DTT6Y2 twX3lqWiiBZD3RJfKPl4/ipJ3dHTFtyxzDlior/g7f9gh9L50ZRUYizAJf1oVKjjVLRd ys5g== X-Gm-Message-State: AOAM530+7BkI31P5yFsF/niZJDxOJyCw3AA4ZyHtrQhEjUwMUzn8tdj6 xYaDo3bswJwFs3dg4LQlBSxTy32k7xUOAQ== X-Google-Smtp-Source: ABdhPJxwm3kLJPptggDG2RO5Tjk5OHMSKE1fExFYfWB0/fupslRtVvVWhlmtzLwgbvYJAdn2Gdrz9Q== X-Received: by 2002:a05:651c:554:: with SMTP id q20mr3224240ljp.390.1628778159379; Thu, 12 Aug 2021 07:22:39 -0700 (PDT) From: Anastasiia Lukianenko To: xen-devel@lists.xenproject.org Cc: oleksandr_tyshchenko@epam.com, artem_mygaiev@epam.com, rahul.singh@arm.com, bertrand.marquis@arm.com, oleksandr_andrushchenko@epam.com, roger.pau@citrix.com, julien@xen.org, sstabellini@kernel.org, andr2000@gmail.com, vicooodin@gmail.com, Anastasiia Lukianenko Subject: [PATCH 6/9] tools/libs/light: Add "read" command to xl pcid and libxl PCI Date: Thu, 12 Aug 2021 17:22:27 +0300 Message-Id: <20210812142230.19353-7-vicooodin@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210812142230.19353-1-vicooodin@gmail.com> References: <20210812142230.19353-1-vicooodin@gmail.com> X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628778203413100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Anastasiia Lukianenko Libxl PCI can use xl pcid server to read integer value from the given path. Signed-off-by: Anastasiia Lukianenko --- tools/include/pcid.h | 1 + tools/libs/light/libxl_pci.c | 125 ++++++++++++++++++---------------- tools/libs/light/libxl_pcid.c | 50 ++++++++++++++ 3 files changed, 119 insertions(+), 57 deletions(-) diff --git a/tools/include/pcid.h b/tools/include/pcid.h index 935d99b186..f39011ecb8 100644 --- a/tools/include/pcid.h +++ b/tools/include/pcid.h @@ -32,6 +32,7 @@ #define PCID_CMD_DIR_ID "dir_id" =20 #define PCID_CMD_WRITE "write" +#define PCID_CMD_READ_HEX "read_hex" #define PCID_CMD_PCI_PATH "pci_path" #define PCID_CMD_PCI_INFO "pci_info" =20 diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c index 03ce42dec3..d5ddca4964 100644 --- a/tools/libs/light/libxl_pci.c +++ b/tools/libs/light/libxl_pci.c @@ -634,83 +634,94 @@ static int sysfs_dev_unbind(libxl__gc *gc, libxl_devi= ce_pci *pci, static uint16_t sysfs_dev_get_vendor(libxl__gc *gc, libxl_device_pci *pci) { char *pci_device_vendor_path =3D - GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/vendor", - pci->domain, pci->bus, pci->dev, pci->func); - uint16_t read_items; + GCSPRINTF("/"PCI_BDF"/vendor", pci->domain, pci->bus, + pci->dev, pci->func); uint16_t pci_device_vendor; + struct vchan_info *vchan; + libxl__json_object *args =3D NULL, *result =3D NULL; =20 - FILE *f =3D fopen(pci_device_vendor_path, "r"); - if (!f) { - LOGE(ERROR, - "pci device "PCI_BDF" does not have vendor attribute", - pci->domain, pci->bus, pci->dev, pci->func); - return 0xffff; - } - read_items =3D fscanf(f, "0x%hx\n", &pci_device_vendor); - fclose(f); - if (read_items !=3D 1) { - LOGE(ERROR, - "cannot read vendor of pci device "PCI_BDF, - pci->domain, pci->bus, pci->dev, pci->func); - return 0xffff; - } + vchan =3D pci_prepare_vchan(gc); + if (!vchan) + goto fail; + libxl__vchan_param_add_string(gc, &args, PCID_CMD_PCI_INFO, + pci_device_vendor_path); + libxl__vchan_param_add_string(gc, &args, PCID_CMD_DIR_ID, + PCID_PCI_DEV); + result =3D vchan_send_command(gc, vchan, PCID_CMD_READ_HEX, args); + if (!result) + goto fail; + + pci_device_vendor =3D libxl__json_object_get_integer(result); =20 return pci_device_vendor; + +fail: + LOGE(ERROR, + "cannot read vendor of pci device "PCI_BDF, + pci->domain, pci->bus, pci->dev, pci->func); + return 0xffff; } =20 static uint16_t sysfs_dev_get_device(libxl__gc *gc, libxl_device_pci *pci) { char *pci_device_device_path =3D - GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/device", - pci->domain, pci->bus, pci->dev, pci->func); - uint16_t read_items; + GCSPRINTF("/"PCI_BDF"/device", pci->domain, pci->bus, + pci->dev, pci->func); uint16_t pci_device_device; + struct vchan_info *vchan; + libxl__json_object *args =3D NULL, *result =3D NULL; =20 - FILE *f =3D fopen(pci_device_device_path, "r"); - if (!f) { - LOGE(ERROR, - "pci device "PCI_BDF" does not have device attribute", - pci->domain, pci->bus, pci->dev, pci->func); - return 0xffff; - } - read_items =3D fscanf(f, "0x%hx\n", &pci_device_device); - fclose(f); - if (read_items !=3D 1) { - LOGE(ERROR, - "cannot read device of pci device "PCI_BDF, - pci->domain, pci->bus, pci->dev, pci->func); - return 0xffff; - } + vchan =3D pci_prepare_vchan(gc); + if (!vchan) + goto fail; + libxl__vchan_param_add_string(gc, &args, PCID_CMD_PCI_INFO, + pci_device_device_path); + libxl__vchan_param_add_string(gc, &args, PCID_CMD_DIR_ID, + PCID_PCI_DEV); + result =3D vchan_send_command(gc, vchan, PCID_CMD_READ_HEX, args); + if (!result) + goto fail; + + pci_device_device =3D libxl__json_object_get_integer(result); =20 return pci_device_device; + +fail: + LOGE(ERROR, + "cannot read device of pci device "PCI_BDF, + pci->domain, pci->bus, pci->dev, pci->func); + return 0xffff; } =20 static int sysfs_dev_get_class(libxl__gc *gc, libxl_device_pci *pci, unsigned long *class) { - char *pci_device_class_path =3D GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/cla= ss", - pci->domain, pci->bus, pci->dev, pci->func); - int read_items, ret =3D 0; + char *pci_device_class_path =3D GCSPRINTF("/"PCI_BDF"/class", + pci->domain, pci->bus, + pci->dev, pci->func); + struct vchan_info *vchan; + libxl__json_object *args =3D NULL, *result =3D NULL; =20 - FILE *f =3D fopen(pci_device_class_path, "r"); - if (!f) { - LOGE(ERROR, - "pci device "PCI_BDF" does not have class attribute", - pci->domain, pci->bus, pci->dev, pci->func); - ret =3D ERROR_FAIL; - goto out; - } - read_items =3D fscanf(f, "0x%lx\n", class); - fclose(f); - if (read_items !=3D 1) { - LOGE(ERROR, - "cannot read class of pci device "PCI_BDF, - pci->domain, pci->bus, pci->dev, pci->func); - ret =3D ERROR_FAIL; - } + vchan =3D pci_prepare_vchan(gc); + if (!vchan) + goto fail; + libxl__vchan_param_add_string(gc, &args, PCID_CMD_PCI_INFO, + pci_device_class_path); + libxl__vchan_param_add_string(gc, &args, PCID_CMD_DIR_ID, + PCID_PCI_DEV); + result =3D vchan_send_command(gc, vchan, PCID_CMD_READ_HEX, args); + if (!result) + goto fail; =20 -out: - return ret; + *class =3D libxl__json_object_get_integer(result); + + return 0; + +fail: + LOGE(ERROR, + "cannot read class of pci device "PCI_BDF, + pci->domain, pci->bus, pci->dev, pci->func); + return ERROR_FAIL; } =20 /* diff --git a/tools/libs/light/libxl_pcid.c b/tools/libs/light/libxl_pcid.c index ee4c832779..0f736c68af 100644 --- a/tools/libs/light/libxl_pcid.c +++ b/tools/libs/light/libxl_pcid.c @@ -154,6 +154,54 @@ out: return result; } =20 +static libxl__json_object *process_read_hex_cmd(libxl__gc *gc, + const struct libxl__json_o= bject *resp) +{ + libxl__json_object *result =3D NULL; + const struct libxl__json_object *args, *dir_id, *pci_info; + char *full_path; + uint16_t read_items; + long long read_number; + + args =3D libxl__json_map_get(PCID_MSG_FIELD_ARGS, resp, JSON_MAP); + if (!args) + goto out; + dir_id =3D libxl__json_map_get(PCID_CMD_DIR_ID, args, JSON_ANY); + if (!dir_id) + goto out; + pci_info =3D libxl__json_map_get(PCID_CMD_PCI_INFO, args, JSON_ANY); + if (!pci_info) + goto out; + + if (strcmp(PCID_PCI_DEV, dir_id->u.string) =3D=3D 0) + full_path =3D libxl__sprintf(gc, SYSFS_PCI_DEV"%s", pci_info->u.st= ring); + else + full_path =3D pci_info->u.string; + + FILE *f =3D fopen(full_path, "r"); + if (!f) { + LOGE(ERROR, "PCI device %s does not have needed attribute\n", + full_path); + goto out; + } + read_items =3D fscanf(f, "0x%llx\n", &read_number); + fclose(f); + if (read_items !=3D 1) { + LOGE(ERROR, "Cannot read attribute of pci device %s\n", full_path); + goto out; + } + + result =3D libxl__json_object_alloc(gc, JSON_INTEGER); + if (!result) { + LOGE(ERROR, "Memory allocation failed\n"); + goto out; + } + result->u.i =3D read_number; + +out: + return result; +} + static int pcid_handle_message(libxl__gc *gc, const libxl__json_object *re= quest, libxl__json_object **result) { @@ -171,6 +219,8 @@ static int pcid_handle_message(libxl__gc *gc, const lib= xl__json_object *request, *result =3D process_ls_cmd(gc, request); else if (strcmp(PCID_CMD_WRITE, command_name) =3D=3D 0) *result =3D process_write_cmd(gc, request); + else if (strcmp(command_name, PCID_CMD_READ_HEX) =3D=3D 0) + *result =3D process_read_hex_cmd(gc, request); else return ERROR_NOTFOUND; =20 --=20 2.17.1 From nobody Thu May 2 11:40:14 2024 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=1628778212; cv=none; d=zohomail.com; s=zohoarc; b=kxJVbau+7z393oht22MBC8IPFwrvTxa4apRe3XR6UTSec/VjbNXXMbYxrsdzzo5sH8Csy4Afa2+a8jAAQ6gOXnfQR7obkWM492AR6aX0qyon3o29nQVLIyCLZbq4ni6HMSvGcNKHY3vW8zAaQhBR9OoiqpY4zhOAQsWYMF/dAlY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628778212; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=mRu1nhbLdCG3tjQcY0lNShHW8lgPAtPkcfN4T//o5X4=; b=mMoC9/QClDJ/toib6K/G7NPEZUYQgtgIr7p4G9cN7B5wavsoLUQp6dqbhHSZQ+GDLxnT4e1SJzREOsgDk6UjhqomBwLZQLOHuK9AWHztDiYXaZAi1ZCREUEmtXeG6Z4gj5cYM07V0RzPtLAa5G3zInillt8uaOnI0/uBq5AlDtg= 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 1628778212191998.0888557060902; Thu, 12 Aug 2021 07:23:32 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.166478.303876 (Exim 4.92) (envelope-from ) id 1mEBc5-0006F4-Ef; Thu, 12 Aug 2021 14:23:17 +0000 Received: by outflank-mailman (output) from mailman id 166478.303876; Thu, 12 Aug 2021 14:23:17 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mEBc5-0006EQ-9g; Thu, 12 Aug 2021 14:23:17 +0000 Received: by outflank-mailman (input) for mailman id 166478; Thu, 12 Aug 2021 14:23:16 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mEBc3-0002Q2-Vu for xen-devel@lists.xenproject.org; Thu, 12 Aug 2021 14:23:16 +0000 Received: from mail-lf1-x135.google.com (unknown [2a00:1450:4864:20::135]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 8c57d227-a9ca-4282-96d4-222312233d86; Thu, 12 Aug 2021 14:22:43 +0000 (UTC) Received: by mail-lf1-x135.google.com with SMTP id z20so13830899lfd.2 for ; Thu, 12 Aug 2021 07:22:42 -0700 (PDT) Received: from localhost.localdomain (host-176-36-213-80.b024.la.net.ua. [176.36.213.80]) by smtp.gmail.com with ESMTPSA id s16sm329678ljj.48.2021.08.12.07.22.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Aug 2021 07:22:40 -0700 (PDT) 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: 8c57d227-a9ca-4282-96d4-222312233d86 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=mRu1nhbLdCG3tjQcY0lNShHW8lgPAtPkcfN4T//o5X4=; b=TsmtWtJS9CZTcjwYV8nlT64CwRYGtU17kWngeHIA6EfJAvRvqjyWr2IVNiXsoAQ7+Y ucooQelHRR3+fkHW2uBzPe7yAVyJ7saiqYttALbKFdARwjs32v17kZ9iUycLN71+IPWf V2fzgKaQUUfL8fPAN4WihEUHFxZjXfyWX+2AJsrnANjC47vo8QUvnNESvCYxu+/IUtah JWCWlEHca2MsB3LayUHVY6rNHfi5kD9gvx3ehxrbP2gZksVruQEghkVPIzq3CALgT3Kl Tzq6UqiGCKkOvkDfh+1waTPQJnt9OjB4KOz32VxAQmcAfBNgTWdkrVDAOEmu7VEhxzvm u16w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=mRu1nhbLdCG3tjQcY0lNShHW8lgPAtPkcfN4T//o5X4=; b=bott3QLs4vuI5EGwIvFcfIRruscfbKm62eL1qUK+W6t9yi3+XzGFsgTLFFQOSAsF7+ 9FPzTN56sqqfcIsFvBgoj8GSJg+lWIF+jQd5y33vO2VN0Irl6ie+ODHEW7+uMDPuq+zP QHBBwDasux2xJpUlIQfub7KbkzlsdTAjR1/ppFOZgcrVK3wMjg4naeQwlpw5hJJscsoa h6Zvie4r2Lb8LRRfZSB8ATIslMoTZr+A7o2vC8GMWDhnHVeK6n3hhOXyyTgaZ1z3LKsh kZfujjTygzgTpHLSQkQpnPFXJC/pDCXgQgbOy5UtS/y5XEb2UBkP3uL6CRLRvcvVfZId FZHg== X-Gm-Message-State: AOAM532dB1Lm2fofi51sAqgQ5Mcjt5lsirWWK8+0/tZz5DkjyqMdcRk3 G0PdMRomXCvPccfRY6UYo28PKWCjtvKm+w== X-Google-Smtp-Source: ABdhPJyKYLsBa6gX9NJCphm/jfMzDBnLENzVcaDQ0n1Va8qU3IYb0qb5sxZmkrYIh+wdOfIwCJ1JuA== X-Received: by 2002:a05:6512:3884:: with SMTP id n4mr2763937lft.313.1628778160317; Thu, 12 Aug 2021 07:22:40 -0700 (PDT) From: Anastasiia Lukianenko To: xen-devel@lists.xenproject.org Cc: oleksandr_tyshchenko@epam.com, artem_mygaiev@epam.com, rahul.singh@arm.com, bertrand.marquis@arm.com, oleksandr_andrushchenko@epam.com, roger.pau@citrix.com, julien@xen.org, sstabellini@kernel.org, andr2000@gmail.com, vicooodin@gmail.com, Anastasiia Lukianenko Subject: [PATCH 7/9] tools/libs/light: Make Libxl PCI get values from xl pcid instead of libxl side Date: Thu, 12 Aug 2021 17:22:28 +0300 Message-Id: <20210812142230.19353-8-vicooodin@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210812142230.19353-1-vicooodin@gmail.com> References: <20210812142230.19353-1-vicooodin@gmail.com> X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628778212597100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Anastasiia Lukianenko pci_multifunction_check needs to use "ls" and "lstat" commands to get information from sysfs. Add "is_exists" command processing to xl pcid daemon to make possible pci_multifunction_check read directories and check lstat by using xl pcid. Signed-off-by: Anastasiia Lukianenko --- tools/include/pcid.h | 1 + tools/libs/light/libxl_pci.c | 47 ++++++++++++++++++----------------- tools/libs/light/libxl_pcid.c | 47 +++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 23 deletions(-) diff --git a/tools/include/pcid.h b/tools/include/pcid.h index f39011ecb8..5c8efbb435 100644 --- a/tools/include/pcid.h +++ b/tools/include/pcid.h @@ -33,6 +33,7 @@ =20 #define PCID_CMD_WRITE "write" #define PCID_CMD_READ_HEX "read_hex" +#define PCID_CMD_EXISTS "exists" #define PCID_CMD_PCI_PATH "pci_path" #define PCID_CMD_PCI_INFO "pci_info" =20 diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c index d5ddca4964..ab6709890e 100644 --- a/tools/libs/light/libxl_pci.c +++ b/tools/libs/light/libxl_pci.c @@ -1091,45 +1091,46 @@ int libxl_device_pci_assignable_remove(libxl_ctx *c= tx, libxl_device_pci *pci, */ static int pci_multifunction_check(libxl__gc *gc, libxl_device_pci *pci, u= nsigned int *func_mask) { - struct dirent *de; - DIR *dir; - *func_mask =3D 0; + struct vchan_info *vchan; + libxl__json_object *result =3D NULL, *args =3D NULL; + const libxl__json_object *lstat_obj, *dir; + const char *dir_name; + int i; =20 - dir =3D opendir(SYSFS_PCI_DEV); - if ( NULL =3D=3D dir ) { - LOGE(ERROR, "Couldn't open %s", SYSFS_PCI_DEV); + vchan =3D pci_prepare_vchan(gc); + if (!vchan) return -1; - } =20 - while( (de =3D readdir(dir)) ) { + libxl__vchan_param_add_string(gc, &args, PCID_CMD_DIR_ID, PCID_PCI_DEV= ); + result =3D vchan_send_command(gc, vchan, PCID_CMD_LIST, args); + if (!result) + return -1; + + for (i =3D 0; (dir =3D libxl__json_array_get(result, i)); i++) { + dir_name =3D libxl__json_object_get_string(dir); unsigned dom, bus, dev, func; - struct stat st; char *path; =20 - if ( sscanf(de->d_name, PCI_BDF, &dom, &bus, &dev, &func) !=3D 4 ) + if (sscanf(dir_name, PCI_BDF, &dom, &bus, &dev, &func) !=3D 4) continue; - if ( pci->domain !=3D dom ) + if (pci->domain !=3D dom) continue; - if ( pci->bus !=3D bus ) + if (pci->bus !=3D bus) continue; - if ( pci->dev !=3D dev ) + if (pci->dev !=3D dev) continue; =20 - path =3D GCSPRINTF("%s/" PCI_BDF, SYSFS_PCIBACK_DRIVER, dom, bus, = dev, func); - if ( lstat(path, &st) ) { - if ( errno =3D=3D ENOENT ) - LOG(ERROR, PCI_BDF " is not assigned to pciback driver", - dom, bus, dev, func); - else - LOGE(ERROR, "Couldn't lstat %s", path); - closedir(dir); + path =3D GCSPRINTF("/" PCI_BDF, dom, bus, dev, func); + libxl__vchan_param_add_string(gc, &args, PCID_CMD_DIR_ID, PCID_PCI= BACK_DRIVER); + libxl__vchan_param_add_string(gc, &args, PCID_CMD_PCI_INFO, path); + lstat_obj =3D vchan_send_command(gc, vchan, PCID_CMD_EXISTS, args); + if (!lstat_obj) return -1; - } + (*func_mask) |=3D (1 << func); } =20 - closedir(dir); return 0; } =20 diff --git a/tools/libs/light/libxl_pcid.c b/tools/libs/light/libxl_pcid.c index 0f736c68af..28d773f48d 100644 --- a/tools/libs/light/libxl_pcid.c +++ b/tools/libs/light/libxl_pcid.c @@ -34,6 +34,10 @@ #include #include =20 +#include +#include +#include + #define DOM0_ID 0 =20 static struct libxl__json_object *process_ls_cmd(libxl__gc *gc, @@ -202,6 +206,47 @@ out: return result; } =20 +static libxl__json_object *process_exists_cmd(libxl__gc *gc, + const struct libxl__json_obj= ect *resp) +{ + libxl__json_object *result =3D NULL; + const struct libxl__json_object *args, *pci_path, *pci_info; + char *full_path; + struct stat st; + + args =3D libxl__json_map_get(PCID_MSG_FIELD_ARGS, resp, JSON_MAP); + if (!args) + goto out; + pci_path =3D libxl__json_map_get(PCID_CMD_DIR_ID, args, JSON_ANY); + if (!pci_path) + goto out; + pci_info =3D libxl__json_map_get(PCID_CMD_PCI_INFO, args, JSON_ANY); + if (!pci_info) + goto out; + if (strcmp(pci_path->u.string, PCID_PCIBACK_DRIVER) =3D=3D 0) + full_path =3D libxl__sprintf(gc, SYSFS_PCIBACK_DRIVER"%s", pci_inf= o->u.string); + else + full_path =3D pci_info->u.string; + + if (lstat(full_path, &st)) { + if (errno =3D=3D ENOENT) + LOGE(ERROR, "%s is not assigned to pciback driver", full_path); + else + LOGE(ERROR, "Couldn't lstat %s", full_path); + goto out; + } + + result =3D libxl__json_object_alloc(gc, JSON_STRING); + if (!result) { + LOGE(ERROR, "Memory allocation failed\n"); + goto out; + } + result->u.string =3D pci_path->u.string; + +out: + return result; +} + static int pcid_handle_message(libxl__gc *gc, const libxl__json_object *re= quest, libxl__json_object **result) { @@ -221,6 +266,8 @@ static int pcid_handle_message(libxl__gc *gc, const lib= xl__json_object *request, *result =3D process_write_cmd(gc, request); else if (strcmp(command_name, PCID_CMD_READ_HEX) =3D=3D 0) *result =3D process_read_hex_cmd(gc, request); + else if (strcmp(command_name, PCID_CMD_EXISTS) =3D=3D 0) + *result =3D process_exists_cmd(gc, request); else return ERROR_NOTFOUND; =20 --=20 2.17.1 From nobody Thu May 2 11:40:14 2024 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=1628778206; cv=none; d=zohomail.com; s=zohoarc; b=STso1s1pKLog81ImHJyQNhOy+OJf2eNpT/7bzkyJlZvCmT/KDGSwozrGoxqgxRM1yJKGLIJS/fY+zwaLQWy54gqfGmXyQ8Fxub14d/sb+mzNTuhptEvnVSs26KYFK2bfm8M/eOdPlLVCvAwwO9HdEWGN9op1Ah9jPer2WmR3S8Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628778206; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=k2neQYpzxMDHJ24D/F3vy55NfEdiyR+99TzkM/Q5nvs=; b=DM/YjlslDmrVdVo7YGzyop4HUPl8n0dhdRZfONjQ/65KNIwYJrUq08Xnn0A6lZAAMUv37y66plOOMHuc4yCIkWq+JNOFdfJamTqakCSZZ31JqDZQAF6nkcD2e9GuivnACT27mXfW/P8t0zOFZQ7Ulf7nSMF/dTQU9KokR3LX5yY= 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 16287782061807.01555406736793; Thu, 12 Aug 2021 07:23:26 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.166471.303866 (Exim 4.92) (envelope-from ) id 1mEBc1-0005ct-3s; Thu, 12 Aug 2021 14:23:13 +0000 Received: by outflank-mailman (output) from mailman id 166471.303866; Thu, 12 Aug 2021 14:23:13 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mEBc0-0005cY-Ui; Thu, 12 Aug 2021 14:23:12 +0000 Received: by outflank-mailman (input) for mailman id 166471; Thu, 12 Aug 2021 14:23:11 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mEBby-0002Q2-VW for xen-devel@lists.xenproject.org; Thu, 12 Aug 2021 14:23:11 +0000 Received: from mail-lj1-x235.google.com (unknown [2a00:1450:4864:20::235]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 31167468-c47a-46b5-96f1-b5e6333f52bd; Thu, 12 Aug 2021 14:22:42 +0000 (UTC) Received: by mail-lj1-x235.google.com with SMTP id h11so10767961ljo.12 for ; Thu, 12 Aug 2021 07:22:42 -0700 (PDT) Received: from localhost.localdomain (host-176-36-213-80.b024.la.net.ua. [176.36.213.80]) by smtp.gmail.com with ESMTPSA id s16sm329678ljj.48.2021.08.12.07.22.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Aug 2021 07:22:40 -0700 (PDT) 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: 31167468-c47a-46b5-96f1-b5e6333f52bd DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=k2neQYpzxMDHJ24D/F3vy55NfEdiyR+99TzkM/Q5nvs=; b=YjO7J84WGgQnejYXEteDMmXUZ1mtrw3o2iQ86nTaqnlb5BKnXNta8AsjUCrp/Q3Qwk m9nFOKN0DslGnwUphW0JynglEdFbJq1UZ53QKnUvWhaUNSxXL8RpFInJmN4+8dd57hxl EG4j9HUiGs25BGAAW5O8UAGBB76D86QC3U0ZWJAqHUqceFKtpNmphEy6vDymaflHND+U C+B1CXmhjDFxC6Z+v9WomoXXf5iDkM2ildaXHUCj2j0pnc9glOMftnuM7V3YcdEpHH9E U22TpL9nCMLrBBuBRhm5BXIPRY54etcH2oq0k5F56JZ4nVLOczJDVTQFLNNx3P7ZtySv KOVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=k2neQYpzxMDHJ24D/F3vy55NfEdiyR+99TzkM/Q5nvs=; b=eFhGTCjmGlzwFrueXFCw35Pjr+AB3Sgcx8mc7X/kO2TsyugEmEkMxjEWPwe2bqmVU9 BQeJ174RiiE0pl1x3QfvQE6MYpOI/7iSnY4k7tbJ5lGiFxpOyIk7dRviSEtwKrFOQWBn UIfGGsqxGN2pYIkGh1YI0TObqsHB0CwuUthfJ+me0xSbkjzX7z+y9d+xY+AeYd7P+ttz lohktB1qIMeZhnMX/ZCxSHU7g9QbXJ7m/7NGOgiBHDSs7MKzV88JWnk4++5HwTMD9D0n NpWrtgDxd3qjTp+bSppUruD+9dWGSYgxXn0KwuG6DYRQFFRpfrv666BhWEUwcs7xjoCz RN+w== X-Gm-Message-State: AOAM532uA3Kpow/9ctS7IznpOKqWpSFhVdf3pn2gMFxTUQ93YojIlxyT 5i1apGYbWJTWnZ/OfEm3NXCu0LDV/xntVA== X-Google-Smtp-Source: ABdhPJz5IVi2+4nToSisUryNM41G0uPelSFULQEuB4cFeY3QvOLyCJIxc+cJSaoWywqkceI3PMVlCQ== X-Received: by 2002:a2e:9ec8:: with SMTP id h8mr3111661ljk.454.1628778161267; Thu, 12 Aug 2021 07:22:41 -0700 (PDT) From: Anastasiia Lukianenko To: xen-devel@lists.xenproject.org Cc: oleksandr_tyshchenko@epam.com, artem_mygaiev@epam.com, rahul.singh@arm.com, bertrand.marquis@arm.com, oleksandr_andrushchenko@epam.com, roger.pau@citrix.com, julien@xen.org, sstabellini@kernel.org, andr2000@gmail.com, vicooodin@gmail.com, Anastasiia Lukianenko Subject: [PATCH 8/9] tools/libs/light: Add "unbind" and "read resources" commands to libxl PCI and xl pcid Date: Thu, 12 Aug 2021 17:22:29 +0300 Message-Id: <20210812142230.19353-9-vicooodin@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210812142230.19353-1-vicooodin@gmail.com> References: <20210812142230.19353-1-vicooodin@gmail.com> X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628778208056100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Anastasiia Lukianenko Send requests to xl pcid from libxl PCI to prevent the libxl PCI from reading / writing from / to the local sysfs directly. "Unbind" command returns driver's path or "nolstat" result in success and NULL if writing to PCI path failed. "Read resources" command returns an array of PCI resources - start, end and flags in success, NULL if getting resources from PCI path failed. Signed-off-by: Anastasiia Lukianenko --- tools/include/pcid.h | 7 +++ tools/libs/light/libxl_pci.c | 99 ++++++++++++++++++-------------- tools/libs/light/libxl_pcid.c | 105 ++++++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+), 42 deletions(-) diff --git a/tools/include/pcid.h b/tools/include/pcid.h index 5c8efbb435..3153bafb19 100644 --- a/tools/include/pcid.h +++ b/tools/include/pcid.h @@ -29,11 +29,13 @@ #define PCID_MSG_FIELD_ARGS "arguments" =20 #define PCID_CMD_LIST "ls" +#define PCID_CMD_UNBIND "unbind" #define PCID_CMD_DIR_ID "dir_id" =20 #define PCID_CMD_WRITE "write" #define PCID_CMD_READ_HEX "read_hex" #define PCID_CMD_EXISTS "exists" +#define PCID_CMD_READ_RESOURCES "read_resources" #define PCID_CMD_PCI_PATH "pci_path" #define PCID_CMD_PCI_INFO "pci_info" =20 @@ -44,12 +46,17 @@ =20 #if defined(__linux__) #define SYSFS_PCIBACK_DRIVER "/sys/bus/pci/drivers/pciback" +#define SYSFS_PCI_DEV "/sys/bus/pci/devices" #endif =20 #define PCI_INFO_PATH "/libxl/pci" #define PCI_BDF_XSPATH "%04x-%02x-%02x-%01x" #define PCI_BDF "%04x:%02x:%02x.%01x" =20 +#define RESOURCE_START "start" +#define RESOURCE_END "end" +#define RESOURCE_FLAGS "flags" + int libxl_pcid_process(libxl_ctx *ctx); =20 #endif /* PCID_H */ diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c index ab6709890e..3d9bf4830b 100644 --- a/tools/libs/light/libxl_pci.c +++ b/tools/libs/light/libxl_pci.c @@ -599,34 +599,32 @@ void libxl_device_pci_assignable_list_free(libxl_devi= ce_pci *list, int num) static int sysfs_dev_unbind(libxl__gc *gc, libxl_device_pci *pci, char **driver_path) { - char *spath, *pci_path, *dp =3D NULL; - struct stat st; + char *pci_path, *dp =3D NULL; =20 pci_path =3D GCSPRINTF("/"PCI_BDF"/driver", pci->domain, pci->bus, pci->dev, pci->func); + char *pci_info; + struct vchan_info *vchan; + libxl__json_object *args =3D NULL, *result =3D NULL; =20 - spath =3D GCSPRINTF(SYSFS_PCI_DEV"%s", pci_path); - if ( !lstat(spath, &st) ) { - /* Find the canonical path to the driver. */ - dp =3D libxl__zalloc(gc, PATH_MAX); - dp =3D realpath(spath, dp); - if ( !dp ) { - LOGE(ERROR, "realpath() failed"); - return -1; - } - - LOG(DEBUG, "Driver re-plug path: %s", dp); + vchan =3D pci_prepare_vchan(gc); + if (!vchan) + return ERROR_FAIL; =20 - /* Unbind from the old driver */ - spath =3D GCSPRINTF("%s/unbind", dp); - if (sysfs_write_bdf(gc, SYSFS_PCI_DEV, pci_path, pci) < 0) { - LOGE(ERROR, "Couldn't unbind device"); - return -1; - } + pci_info =3D GCSPRINTF(PCI_BDF, pci->domain, pci->bus, pci->dev, pci->= func); + libxl__vchan_param_add_string(gc, &args, PCID_CMD_PCI_PATH, pci_path); + libxl__vchan_param_add_string(gc, &args, PCID_CMD_PCI_INFO, pci_info); + result =3D vchan_send_command(gc, vchan, PCID_CMD_UNBIND, args); + if (!result) { + LOGE(WARN, "Write to %s%s failed\n", SYSFS_PCI_DEV, pci_path); + return -1; } =20 - if ( driver_path ) + if (driver_path) { + if (strcmp(result->u.string, "nolstat") !=3D 0) + dp =3D (char *)libxl__json_object_get_string(result); *driver_path =3D dp; + } =20 return 0; } @@ -1488,8 +1486,6 @@ static void pci_add_dm_done(libxl__egc *egc, STATE_AO_GC(pas->aodev->ao); libxl_ctx *ctx =3D libxl__gc_owner(gc); libxl_domid domid =3D pas->pci_domid; - char *sysfs_path; - FILE *f; unsigned long long start, end, flags, size; int irq, i; int r; @@ -1510,20 +1506,38 @@ static void pci_add_dm_done(libxl__egc *egc, if (isstubdom) starting =3D false; =20 - sysfs_path =3D GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/resource", pci->doma= in, - pci->bus, pci->dev, pci->func); - f =3D fopen(sysfs_path, "r"); - start =3D end =3D flags =3D size =3D 0; - irq =3D 0; + struct vchan_info *vchan; + libxl__json_object *result =3D NULL, *args =3D NULL; + const libxl__json_object *addr, *node; + char *resource_path, *irq_path; =20 - if (f =3D=3D NULL) { - LOGED(ERROR, domainid, "Couldn't open %s", sysfs_path); + vchan =3D pci_prepare_vchan(gc); + if (!vchan) + goto out; + + resource_path =3D GCSPRINTF("/"PCI_BDF"/resource", pci->domain, + pci->bus, pci->dev, pci->func); + libxl__vchan_param_add_string(gc, &args, PCID_CMD_PCI_INFO, resource_p= ath); + result =3D vchan_send_command(gc, vchan, PCID_CMD_READ_RESOURCES, args= ); + if (!result) { + LOGED(ERROR, domainid, "Couldn't get resources from %s", resource_= path); rc =3D ERROR_FAIL; goto out; } + + start =3D end =3D flags =3D size =3D 0; + irq =3D 0; + for (i =3D 0; i < PROC_PCI_NUM_RESOURCES; i++) { - if (fscanf(f, "0x%llx 0x%llx 0x%llx\n", &start, &end, &flags) !=3D= 3) - continue; + node =3D libxl__json_array_get(result, i); + + addr =3D libxl__json_map_get(RESOURCE_START, node, JSON_INTEGER); + start =3D libxl__json_object_get_integer(addr); + addr =3D libxl__json_map_get(RESOURCE_END, node, JSON_INTEGER); + end =3D libxl__json_object_get_integer(addr); + addr =3D libxl__json_map_get(RESOURCE_FLAGS, node, JSON_INTEGER); + flags =3D libxl__json_object_get_integer(addr); + size =3D end - start + 1; if (start) { if (flags & PCI_BAR_IO) { @@ -1532,7 +1546,6 @@ static void pci_add_dm_done(libxl__egc *egc, LOGED(ERROR, domainid, "xc_domain_ioport_permission 0x%llx/0x%llx (erro= r %d)", start, size, r); - fclose(f); rc =3D ERROR_FAIL; goto out; } @@ -1543,27 +1556,30 @@ 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 =3D ERROR_FAIL; goto out; } } } } - fclose(f); - sysfs_path =3D GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domain, - pci->bus, pci->dev, pci->func); - f =3D fopen(sysfs_path, "r"); - if (f =3D=3D NULL) { - LOGED(ERROR, domainid, "Couldn't open %s", sysfs_path); + + irq_path =3D GCSPRINTF("/"PCI_BDF"/irq", pci->domain, + pci->bus, pci->dev, pci->func); + libxl__vchan_param_add_string(gc, &args, PCID_CMD_PCI_INFO, irq_path); + libxl__vchan_param_add_string(gc, &args, PCID_CMD_DIR_ID, PCID_PCI_DEV= ); + result =3D vchan_send_command(gc, vchan, PCID_CMD_READ_HEX, args); + if (!result) { + LOGED(ERROR, domainid, "Couldn't get irq from %s", irq_path); + rc =3D ERROR_FAIL; goto out_no_irq; } - if ((fscanf(f, "%u", &irq) =3D=3D 1) && irq) { + + irq =3D libxl__json_object_get_integer(result); + if (irq) { r =3D xc_physdev_map_pirq(ctx->xch, domid, irq, &irq); if (r < 0) { LOGED(ERROR, domainid, "xc_physdev_map_pirq irq=3D%d (error=3D= %d)", irq, r); - fclose(f); rc =3D ERROR_FAIL; goto out; } @@ -1578,7 +1594,6 @@ static void pci_add_dm_done(libxl__egc *egc, } #endif } - fclose(f); =20 /* Don't restrict writes to the PCI config space from this VM */ if (pci->permissive) { diff --git a/tools/libs/light/libxl_pcid.c b/tools/libs/light/libxl_pcid.c index 28d773f48d..7dd8f53f78 100644 --- a/tools/libs/light/libxl_pcid.c +++ b/tools/libs/light/libxl_pcid.c @@ -247,6 +247,107 @@ out: return result; } =20 +static libxl__json_object *process_read_rsc_cmd(libxl__gc *gc, + const struct libxl__json_o= bject *resp) +{ + libxl__json_object *result =3D NULL; + const libxl__json_object *args, *pci_info; + libxl__json_object *node; + unsigned long long start, end, flags; + int i; + char *sysfs_path; + FILE *f; + + args =3D libxl__json_map_get(PCID_MSG_FIELD_ARGS, resp, JSON_MAP); + if (!args) + goto out; + pci_info =3D libxl__json_map_get(PCID_CMD_PCI_INFO, args, JSON_ANY); + if (!pci_info) + goto out; + + sysfs_path =3D libxl__sprintf(gc, SYSFS_PCI_DEV"%s", pci_info->u.strin= g); + f =3D fopen(sysfs_path, "r"); + if (!f) { + LOGE(ERROR, "Failed to open %s\n", sysfs_path); + goto out; + } + + result =3D libxl__json_object_alloc(gc, JSON_ARRAY); + if (!result) { + LOGE(ERROR, "Memory allocation failed\n"); + goto fail_mem_alloc; + } + + for (i =3D 0; i < PROC_PCI_NUM_RESOURCES; i++) { + if (fscanf(f, "0x%llx 0x%llx 0x%llx\n", &start, &end, &flags) !=3D= 3) + continue; + + node =3D libxl__json_object_alloc(gc, JSON_MAP); + if (!node) { + LOGE(ERROR, "Memory allocation failed\n"); + goto fail_mem_alloc; + } + libxl__vchan_param_add_integer(gc, &node, RESOURCE_START, start); + libxl__vchan_param_add_integer(gc, &node, RESOURCE_END, end); + libxl__vchan_param_add_integer(gc, &node, RESOURCE_FLAGS, flags); + flexarray_append(result->u.array, node); + } + +fail_mem_alloc: + fclose(f); +out: + return result; +} + +static libxl__json_object *process_unbind_cmd(libxl__gc *gc, + const struct libxl__json_obj= ect *resp) +{ + libxl__json_object *result =3D NULL; + const struct libxl__json_object *args, *pci_path, *pci_info; + char *msg, *spath, *new_path, *dp =3D NULL; + struct stat st; + + args =3D libxl__json_map_get(PCID_MSG_FIELD_ARGS, resp, JSON_MAP); + if (!args) + goto out; + pci_info =3D libxl__json_map_get(PCID_CMD_PCI_INFO, args, JSON_ANY); + if (!pci_info) + goto out; + pci_path =3D libxl__json_map_get(PCID_CMD_PCI_PATH, args, JSON_ANY); + if (!pci_path) + goto out; + + spath =3D libxl__sprintf(gc, SYSFS_PCI_DEV"%s", pci_path->u.string); + + if (!lstat(spath, &st)) { + /* Find the canonical path to the driver. */ + dp =3D libxl__zalloc(gc, PATH_MAX); + if (!(realpath(spath, dp))) { + LOGE(ERROR, "realpath() failed\n"); + goto out; + } + msg =3D dp; + /* Unbind from the old driver */ + new_path =3D libxl__sprintf(gc, "%s/unbind", dp); + + if (handle_write_cmd(gc, new_path, pci_info->u.string) !=3D 0) { + LOGE(ERROR, "Couldn't unbind device\n"); + goto out; + } + } else { + msg =3D libxl__sprintf(gc, "nolstat"); + } + result =3D libxl__json_object_alloc(gc, JSON_STRING); + if (!result) { + LOGE(ERROR, "Memory allocation failed\n"); + goto out; + } + result->u.string =3D msg; + +out: + return result; +} + static int pcid_handle_message(libxl__gc *gc, const libxl__json_object *re= quest, libxl__json_object **result) { @@ -268,6 +369,10 @@ static int pcid_handle_message(libxl__gc *gc, const li= bxl__json_object *request, *result =3D process_read_hex_cmd(gc, request); else if (strcmp(command_name, PCID_CMD_EXISTS) =3D=3D 0) *result =3D process_exists_cmd(gc, request); + else if (strcmp(command_name, PCID_CMD_READ_RESOURCES) =3D=3D 0) + *result =3D process_read_rsc_cmd(gc, request); + else if (strcmp(command_name, PCID_CMD_UNBIND) =3D=3D 0) + *result =3D process_unbind_cmd(gc, request); else return ERROR_NOTFOUND; =20 --=20 2.17.1 From nobody Thu May 2 11:40:14 2024 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=1628778216; cv=none; d=zohomail.com; s=zohoarc; b=gQbNp8V1+gyMwFEfJuPbCFW+ICdSUD7zmFr9oZ8zVaIY8hR1w7fpTN29oz68QqAY0OV7TFuPoGjrCBOYuSpA1ICoBzf2yavlJEkWvBw6Jg9cajtvA2+wJgY5qT+Bvi480CZ81+QdqmEoQ+xHPbdyt6fVPmEBSuajopS2u25jruE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628778216; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=fF0wInCslqTLQh+Fbjb4/Af105vv4ysHAegwrEvgNV8=; b=aDzB19lT869vw4ABEBfCgIs2QZcF1nv8ozsLJXHb7hfvCMFHc7XEjVVS5Am05r5TXJQkaexoFdTbGS/3wwQy1Gj1QyVxmEOfiX9f9xiqGkcRUQHCh65B0o16DVtiJJg0+kt4jKcq6+Z1SzUfDgggWcgpzTFJJ9qxB85WMS6TaDg= 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 1628778216244237.7083330805632; Thu, 12 Aug 2021 07:23:36 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.166483.303888 (Exim 4.92) (envelope-from ) id 1mEBcA-0006qT-SW; Thu, 12 Aug 2021 14:23:22 +0000 Received: by outflank-mailman (output) from mailman id 166483.303888; Thu, 12 Aug 2021 14:23:22 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mEBcA-0006qF-O2; Thu, 12 Aug 2021 14:23:22 +0000 Received: by outflank-mailman (input) for mailman id 166483; Thu, 12 Aug 2021 14:23:21 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mEBc8-0002Q2-Vs for xen-devel@lists.xenproject.org; Thu, 12 Aug 2021 14:23:21 +0000 Received: from mail-lf1-x136.google.com (unknown [2a00:1450:4864:20::136]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id db15243a-4c8e-4db6-8c86-b145e29ded7f; Thu, 12 Aug 2021 14:22:43 +0000 (UTC) Received: by mail-lf1-x136.google.com with SMTP id z20so13830951lfd.2 for ; Thu, 12 Aug 2021 07:22:43 -0700 (PDT) Received: from localhost.localdomain (host-176-36-213-80.b024.la.net.ua. [176.36.213.80]) by smtp.gmail.com with ESMTPSA id s16sm329678ljj.48.2021.08.12.07.22.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Aug 2021 07:22:41 -0700 (PDT) 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: db15243a-4c8e-4db6-8c86-b145e29ded7f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=fF0wInCslqTLQh+Fbjb4/Af105vv4ysHAegwrEvgNV8=; b=Ve7AJGKRku9ZVNMC2JNKBULinWP6TztmfyDhHotFGk8aNVwpO/+p1cMs8cnJ4hSlNk 3EaUfi7v8yBA/Gc7Emcp7oV7MC6HDE/QnMYwkfMZClIqEOxUPuxPvsJtZzc/jcYbEV3n a/ISC8EIQBVKHk1YZlUil0Xl+wcZ/MdqwcbVQfEfjlLSjKAQWXU+l/hKFLQ297HhdSu3 nAlpSwzNwMDZ5Ri8uOjByEMV8K4lVZplrKC0DI9BN/iuuj0ullGaMgQfgyEJevzz+9rT rDCVCPVvaL6Iv0TTC8jqDbKOHshkaO8DmxY6CHLK/baX79YOBClGJvvrqqsTGyp7uewC R45w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=fF0wInCslqTLQh+Fbjb4/Af105vv4ysHAegwrEvgNV8=; b=DpbLHqg3MyaYvTaNProZ4hlamKWurjtKpU5kqB4ssC5BuSPsjD6ojdlw0VRq23v+wv eeAelN/d5VUlqQxu9qRgwT8F9095vH4JYb7MdOBdHnjyH9T6vedpDBUIK6Fnuy36R9GC kNTAyk3zSjA0Pov6B0D44OwkT97bljy5d2M4b9cNzfQ0WdtTm4wK0q2nYtQaICcQJWJn BYF1zSGl7ILuX9zoxHrAMcDwU00RUwHGFzgi8zNIqS11nX7pppNHKR7HNSLzW3qCnO67 j/S2XqCgUDMKu55bBQwM6as+uLXh0ZUxiG3QaY+gRx6Jwvcyr/5TaVeBQ1WAbJhei+/i dWnA== X-Gm-Message-State: AOAM532stQiKF9T7wqNXvQkdl73l+dE33JTf2gc/Fpx9cCELwZqhsfUv /48oYgsI2JiN5O8AfbGvTdB0HGSSCgHgOg== X-Google-Smtp-Source: ABdhPJwrGBoi301pR4qIn8rI0NK6QKJqJOB5rjIuuUqbLBpA6iIl2wUOg6077nFXDEMgesdUwig7pA== X-Received: by 2002:a05:6512:118a:: with SMTP id g10mr2708865lfr.491.1628778162524; Thu, 12 Aug 2021 07:22:42 -0700 (PDT) From: Anastasiia Lukianenko To: xen-devel@lists.xenproject.org Cc: oleksandr_tyshchenko@epam.com, artem_mygaiev@epam.com, rahul.singh@arm.com, bertrand.marquis@arm.com, oleksandr_andrushchenko@epam.com, roger.pau@citrix.com, julien@xen.org, sstabellini@kernel.org, andr2000@gmail.com, vicooodin@gmail.com, Anastasiia Lukianenko Subject: [PATCH 9/9] tools/libs/light: Add "reset" and "remove" PCI dev commands to xl pcid daemon and libxl PCI Date: Thu, 12 Aug 2021 17:22:30 +0300 Message-Id: <20210812142230.19353-10-vicooodin@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210812142230.19353-1-vicooodin@gmail.com> References: <20210812142230.19353-1-vicooodin@gmail.com> X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1628778217072100005 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Anastasiia Lukianenko Add "reset" command processing to xl pcid daemon. Make possible sending "reset"/"remove" PCI device requests from libxl PCI to xl pcid daemon instead of using sysfs on libxl side. Signed-off-by: Anastasiia Lukianenko --- tools/include/pcid.h | 1 + tools/libs/light/libxl_pci.c | 94 +++++++++++++++++++---------------- tools/libs/light/libxl_pcid.c | 63 +++++++++++++++++++++++ 3 files changed, 115 insertions(+), 43 deletions(-) diff --git a/tools/include/pcid.h b/tools/include/pcid.h index 3153bafb19..38ea06c602 100644 --- a/tools/include/pcid.h +++ b/tools/include/pcid.h @@ -30,6 +30,7 @@ =20 #define PCID_CMD_LIST "ls" #define PCID_CMD_UNBIND "unbind" +#define PCID_CMD_RESET "reset" #define PCID_CMD_DIR_ID "dir_id" =20 #define PCID_CMD_WRITE "write" diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c index 3d9bf4830b..0498baa47e 100644 --- a/tools/libs/light/libxl_pci.c +++ b/tools/libs/light/libxl_pci.c @@ -1635,37 +1635,26 @@ static int libxl__device_pci_reset(libxl__gc *gc, u= nsigned int domain, unsigned unsigned int dev, unsigned int func) { char *reset; - int fd, rc; - - reset =3D GCSPRINTF("%s/do_flr", SYSFS_PCIBACK_DRIVER); - fd =3D open(reset, O_WRONLY); - if (fd >=3D 0) { - char *buf =3D GCSPRINTF(PCI_BDF, domain, bus, dev, func); - rc =3D 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 !=3D ENOENT) - LOGED(ERROR, domain, "Failed to access pciback path %s", reset); - reset =3D GCSPRINTF("%s/"PCI_BDF"/reset", SYSFS_PCI_DEV, domain, bus, = dev, func); - fd =3D open(reset, O_WRONLY); - if (fd >=3D 0) { - rc =3D 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 =3D=3D 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); + char *buf; + struct vchan_info *vchan; + libxl__json_object *args =3D NULL, *result =3D NULL; + + vchan =3D pci_prepare_vchan(gc); + if (!vchan) + return -1; + + reset =3D GCSPRINTF("%s", "/do_flr"); + buf =3D GCSPRINTF(PCI_BDF, domain, bus, dev, func); + + libxl__vchan_param_add_string(gc, &args, PCID_CMD_PCI_PATH, reset); + libxl__vchan_param_add_string(gc, &args, PCID_CMD_PCI_INFO, buf); + result =3D vchan_send_command(gc, vchan, PCID_CMD_RESET, args); + if (!result) { + LOGD(ERROR, domain, "write to %s returned error", reset); + return -1; } - return -1; + + return 0; } =20 int libxl__device_pci_setdefault(libxl__gc *gc, uint32_t domid, @@ -2085,20 +2074,35 @@ static void do_pci_remove(libxl__egc *egc, pci_remo= ve_state *prs) goto out_fail; } } else { - char *sysfs_path =3D GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/resource",= pci->domain, + char *sysfs_path =3D GCSPRINTF("/"PCI_BDF"/resource", pci->domain, pci->bus, pci->dev, pci->func); - FILE *f =3D fopen(sysfs_path, "r"); unsigned int start =3D 0, end =3D 0, flags =3D 0, size =3D 0; int irq =3D 0; int i; + struct vchan_info *vchan; + libxl__json_object *args =3D NULL, *result =3D NULL; + const libxl__json_object *addr; + int j =3D 0; =20 - if (f =3D=3D NULL) { - LOGED(ERROR, domainid, "Couldn't open %s", sysfs_path); + vchan =3D pci_prepare_vchan(gc); + if (!vchan) + goto out_fail; + libxl__vchan_param_add_string(gc, &args, PCID_CMD_PCI_INFO, sysfs_= path); + result =3D vchan_send_command(gc, vchan, PCID_CMD_READ_RESOURCES, = args); + if (!result) { + LOGED(ERROR, domainid, "Couldn't get resources from %s", sysfs= _path); + rc =3D ERROR_FAIL; goto skip1; } + for (i =3D 0; i < PROC_PCI_NUM_RESOURCES; i++) { - if (fscanf(f, "0x%x 0x%x 0x%x\n", &start, &end, &flags) !=3D 3) - continue; + addr =3D libxl__json_array_get(result, j++); + start =3D libxl__json_object_get_integer(addr); + addr =3D libxl__json_array_get(result, j++); + end =3D libxl__json_object_get_integer(addr); + addr =3D libxl__json_array_get(result, j++); + flags =3D libxl__json_object_get_integer(addr); + size =3D end - start + 1; if (start) { if (flags & PCI_BAR_IO) { @@ -2119,16 +2123,21 @@ static void do_pci_remove(libxl__egc *egc, pci_remo= ve_state *prs) } } } - fclose(f); + skip1: - sysfs_path =3D GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domai= n, + sysfs_path =3D GCSPRINTF("/"PCI_BDF"/irq", pci->domain, pci->bus, pci->dev, pci->func); - f =3D fopen(sysfs_path, "r"); - if (f =3D=3D NULL) { - LOGED(ERROR, domainid, "Couldn't open %s", sysfs_path); + libxl__vchan_param_add_string(gc, &args, PCID_CMD_PCI_INFO, sysfs_= path); + libxl__vchan_param_add_string(gc, &args, PCID_CMD_DIR_ID, PCID_PCI= _DEV); + result =3D vchan_send_command(gc, vchan, PCID_CMD_READ_HEX, args); + if (!result) { + LOGED(ERROR, domainid, "Couldn't get irq from %s", sysfs_path); + rc =3D ERROR_FAIL; goto skip_irq; } - if ((fscanf(f, "%u", &irq) =3D=3D 1) && irq) { + + irq =3D libxl__json_object_get_integer(result); + if (irq) { rc =3D xc_physdev_unmap_pirq(ctx->xch, domid, irq); if (rc < 0) { LOGED(ERROR, domainid, "xc_physdev_unmap_pirq irq=3D%d", i= rq); @@ -2138,7 +2147,6 @@ skip1: LOGED(ERROR, domainid, "xc_domain_irq_permission irq=3D%d"= , irq); } } - fclose(f); } skip_irq: rc =3D 0; diff --git a/tools/libs/light/libxl_pcid.c b/tools/libs/light/libxl_pcid.c index 7dd8f53f78..a4669b9533 100644 --- a/tools/libs/light/libxl_pcid.c +++ b/tools/libs/light/libxl_pcid.c @@ -348,6 +348,67 @@ out: return result; } =20 +static libxl__json_object *process_reset_cmd(libxl__gc *gc, + const struct libxl__json_obje= ct *resp) +{ + libxl__json_object *result =3D NULL; + const libxl__json_object *args, *pci_path, *pci_info; + char *reset; + int rc, fd; + + args =3D libxl__json_map_get(PCID_MSG_FIELD_ARGS, resp, JSON_MAP); + if (!args) + goto out; + pci_info =3D libxl__json_map_get(PCID_CMD_PCI_INFO, args, JSON_ANY); + if (!pci_info) + goto out; + pci_path =3D libxl__json_map_get(PCID_CMD_PCI_PATH, args, JSON_ANY); + + reset =3D libxl__sprintf(gc, SYSFS_PCIBACK_DRIVER"%s", pci_path->u.str= ing); + fd =3D open(reset, O_WRONLY); + if (fd >=3D 0) { + rc =3D write(fd, pci_info->u.string, strlen(pci_info->u.string)); + if (rc < 0) { + LOGE(ERROR, "write to %s returned %d\n", reset, rc); + goto out; + } + close(fd); + goto success; + } + if (errno !=3D ENOENT) + LOGE(ERROR, "Failed to access pciback path %s\n", reset); + + reset =3D libxl__sprintf(gc, "%s/%s/reset", SYSFS_PCI_DEV, pci_info->u= .string); + fd =3D open(reset, O_WRONLY); + if (fd >=3D 0) { + rc =3D write(fd, "1", 1); + if (rc < 0) { + LOGE(ERROR, "write to %s returned %d\n", reset, rc); + goto out; + } + close(fd); + goto success; + } + if (errno =3D=3D ENOENT) + LOGE(ERROR, + "The kernel doesn't support reset from sysfs for PCI devic= e %s\n", + pci_info->u.string); + else + LOGE(ERROR, "Failed to access reset path %s\n", reset); + goto out; + +success: + result =3D libxl__json_object_alloc(gc, JSON_STRING); + if (!result) { + LOGE(ERROR, "Memory allocation failed\n"); + goto out; + } + result->u.string =3D pci_path->u.string; + +out: + return result; +} + static int pcid_handle_message(libxl__gc *gc, const libxl__json_object *re= quest, libxl__json_object **result) { @@ -373,6 +434,8 @@ static int pcid_handle_message(libxl__gc *gc, const lib= xl__json_object *request, *result =3D process_read_rsc_cmd(gc, request); else if (strcmp(command_name, PCID_CMD_UNBIND) =3D=3D 0) *result =3D process_unbind_cmd(gc, request); + else if (strcmp(command_name, PCID_CMD_RESET) =3D=3D 0) + *result =3D process_reset_cmd(gc, request); else return ERROR_NOTFOUND; =20 --=20 2.17.1