From nobody Wed Feb 5 20:02:45 2025 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=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1734107122; cv=none; d=zohomail.com; s=zohoarc; b=M2A9m3lvw/LACTsDyoSFGFvFImRnMGopOyk9mrH7LZ8khUS7Z0izZNFmcm40NUNsIjf3OMyH6Jle+LHlQNwS/c50UYcuaBNOq8csnSzjvF3G6cz3Q1BUQUIkgPKCUTERq5K76MdtQUe4ssqYDVqomxBURrYq0/IUpJAQKWxbo+8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1734107122; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=rbVDjEcqqAdiVmzEYfAl6rmQ+S/MNPFNQOY9L1rY84I=; b=nO61M+SgmG11CMDY3CYQNqjmV/x2tvs8ZrheqiTmAUoaU6vmQKzV9T5hKSxtGL/OGsA9emk3xyDBbA1AaeUcphepyvGnQqdKVgLBswNjPByUilMPv9HliJimjffxxlGtVMlMcx+rZWQLTGnqj/17jO2aplzl+oPgPlJ19pZ638o= 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=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1734107122767908.1136645184332; Fri, 13 Dec 2024 08:25:22 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.856818.1269355 (Exim 4.92) (envelope-from ) id 1tM8TO-0002Uf-Vu; Fri, 13 Dec 2024 16:25:02 +0000 Received: by outflank-mailman (output) from mailman id 856818.1269355; Fri, 13 Dec 2024 16:25: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 1tM8TO-0002UY-Sk; Fri, 13 Dec 2024 16:25:02 +0000 Received: by outflank-mailman (input) for mailman id 856818; Fri, 13 Dec 2024 16:25:01 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tM8TN-0000xX-84 for xen-devel@lists.xenproject.org; Fri, 13 Dec 2024 16:25:01 +0000 Received: from smtp-out1.suse.de (smtp-out1.suse.de [2a07:de40:b251:101:10:150:64:1]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id cba91b08-b96e-11ef-99a3-01e77a169b0f; Fri, 13 Dec 2024 17:24:58 +0100 (CET) Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 41C54210F3; Fri, 13 Dec 2024 16:24:58 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 1C95A137CF; Fri, 13 Dec 2024 16:24:58 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id IQ2LBdpfXGf1SgAAD6G6ig (envelope-from ); Fri, 13 Dec 2024 16:24:58 +0000 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: cba91b08-b96e-11ef-99a3-01e77a169b0f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1734107098; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rbVDjEcqqAdiVmzEYfAl6rmQ+S/MNPFNQOY9L1rY84I=; b=r37vAnbDx5G+zPbik8fdOiHLNVd6P907+OhciNvXjOxeTl6dl5CtC43mX+SKjN+q/1EBb1 pL0SIRj3QDa8T4nfeeHF3XYrX7+GLET2RG+azAKGvTLLDX0c7HCGwaNpuzvc5lvk606WK4 ueFKCUEtDradcJ2OHXWBmKE0YsTk9c4= Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=r37vAnbD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1734107098; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rbVDjEcqqAdiVmzEYfAl6rmQ+S/MNPFNQOY9L1rY84I=; b=r37vAnbDx5G+zPbik8fdOiHLNVd6P907+OhciNvXjOxeTl6dl5CtC43mX+SKjN+q/1EBb1 pL0SIRj3QDa8T4nfeeHF3XYrX7+GLET2RG+azAKGvTLLDX0c7HCGwaNpuzvc5lvk606WK4 ueFKCUEtDradcJ2OHXWBmKE0YsTk9c4= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Anthony PERARD Subject: [PATCH v3 6/7] tools/libs: add a new libxenmanage library Date: Fri, 13 Dec 2024 17:24:20 +0100 Message-ID: <20241213162421.16782-7-jgross@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241213162421.16782-1-jgross@suse.com> References: <20241213162421.16782-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 41C54210F3 X-Spam-Level: X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[gnu.org:url,suse.com:email,suse.com:dkim,suse.com:mid]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; MIME_TRACE(0.00)[0:+]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_TLS_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; RCPT_COUNT_THREE(0.00)[3]; DKIM_TRACE(0.00)[suse.com:+] X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Rspamd-Action: no action X-Spam-Score: -3.01 X-Spam-Flag: NO X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1734107125197116600 Content-Type: text/plain; charset="utf-8" In order to have a stable interface in user land for using stable domctl and possibly later sysctl interfaces, add a new library libxenmanage. Signed-off-by: Juergen Gross --- V1: - new patch V2: - define __XEN_TOOLS__ via Makefile (Anthony PERARD) - use SPDX in header file (Anthony PERARD) - change function name to xenmanage_poll_changed_domain() (Anthony PERARD) - add short library description (Anthony PERARD) - narrow scope of xen_domctl_get_domain_state pointer (Anthony PERARD) --- tools/include/xenmanage.h | 92 ++++++++++++++++ tools/libs/Makefile | 1 + tools/libs/manage/Makefile | 10 ++ tools/libs/manage/Makefile.common | 3 + tools/libs/manage/core.c | 168 +++++++++++++++++++++++++++++ tools/libs/manage/libxenmanage.map | 8 ++ tools/libs/uselibs.mk | 2 + 7 files changed, 284 insertions(+) create mode 100644 tools/include/xenmanage.h create mode 100644 tools/libs/manage/Makefile create mode 100644 tools/libs/manage/Makefile.common create mode 100644 tools/libs/manage/core.c create mode 100644 tools/libs/manage/libxenmanage.map diff --git a/tools/include/xenmanage.h b/tools/include/xenmanage.h new file mode 100644 index 0000000000..5d169693b0 --- /dev/null +++ b/tools/include/xenmanage.h @@ -0,0 +1,92 @@ +/* SPDX-License-Identifier: LGPL-2.1 */ +/* + * Copyright (c) 2024 SUSE Software Solutions Germany GmbH + * + * Interfaces of libxenmanage. + * + * libxenmanage provides management functions for the host using stable + * hypercall interfaces. + */ +#ifndef XENMANAGE_H +#define XENMANAGE_H + +#include + +/* Avoid the need to #include */ +struct xentoollog_logger; + +typedef struct xenmanage_handle xenmanage_handle; + +/* + * Open libxenmanage. + * + * Get a handle of the xenmanage library. The handle is required for all + * further operations of the library. + * Parameters: + * logger: Logging function to use. If NULL logging is done to stder= r. + * open_flags: Only 0 supported. + * Return value: Handle or NULL if error. + */ +xenmanage_handle *xenmanage_open(struct xentoollog_logger *logger, + unsigned int open_flags); + +/* + * Close libxenmanage. + * + * Return a handle of the xenmanage library. + * Parameters: + * hdl: Handle obtained by xenmanage_open(). + * Return value: always 0. + */ +int xenmanage_close(xenmanage_handle *hdl); + +#define XENMANAGE_GETDOMSTATE_STATE_EXIST 0x0001 /* Domain is existin= g. */ +#define XENMANAGE_GETDOMSTATE_STATE_SHUTDOWN 0x0002 /* Shutdown finished= . */ +#define XENMANAGE_GETDOMSTATE_STATE_DYING 0x0004 /* Domain dying. */ +#define XENMANAGE_GETDOMSTATE_STATE_DEAD 0x0008 /* Domain dead. */ + +/* + * Return state information of an existing domain. + * + * Returns the domain state and unique id of the given domain. + * Parameters: + * hdl: handle returned by xenmanage_open() + * domid: domain id of the domain to get the information for + * state: where to store the state (XENMANAGE_GETDOMSTATE_STATE_ fla= gs, + * nothing stored if NULL) + * unique_id: where to store the unique id of the domain (nothing stored= if + * NULL) + * Return value: 0 if information was stored, -1 else (errno is set) + */ +int xenmanage_get_domain_info(xenmanage_handle *hdl, unsigned int domid, + unsigned int *state, uint64_t *unique_id); + +/* + * Return information of a domain having changed state recently. + * + * Returns the domain id, state and unique id of a domain having changed + * state (any of the state bits was modified) since the last time informat= ion + * for that domain was returned by this function. Only usable by callers w= ho + * have registered the VIRQ_DOM_EXC event (normally Xenstore). + * Parameters: + * hdl: handle returned by xenmanage_open() + * domid: where to store the domid of the domain (not NULL) + * state: where to store the state (XENMANAGE_GETDOMSTATE_STATE_ fla= gs, + * nothing stored if NULL) + * unique_id: where to store the unique id of the domain (nothing stored= if + * NULL) + * Return value: 0 if information was stored, -1 else (errno is set) + */ +int xenmanage_poll_changed_domain(xenmanage_handle *hdl, unsigned int *dom= id, + unsigned int *state, uint64_t *unique_id= ); +#endif /* XENMANAGE_H */ + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/tools/libs/Makefile b/tools/libs/Makefile index 1afcd12e2b..d39516c1b3 100644 --- a/tools/libs/Makefile +++ b/tools/libs/Makefile @@ -12,6 +12,7 @@ SUBDIRS-y +=3D devicemodel SUBDIRS-y +=3D ctrl SUBDIRS-y +=3D guest SUBDIRS-y +=3D hypfs +SUBDIRS-y +=3D manage SUBDIRS-y +=3D store SUBDIRS-y +=3D stat SUBDIRS-$(CONFIG_Linux) +=3D vchan diff --git a/tools/libs/manage/Makefile b/tools/libs/manage/Makefile new file mode 100644 index 0000000000..dbfe70d259 --- /dev/null +++ b/tools/libs/manage/Makefile @@ -0,0 +1,10 @@ +XEN_ROOT =3D $(CURDIR)/../../.. +include $(XEN_ROOT)/tools/Rules.mk + +MAJOR =3D 1 +MINOR =3D 0 +version-script :=3D libxenmanage.map + +include Makefile.common + +include $(XEN_ROOT)/tools/libs/libs.mk diff --git a/tools/libs/manage/Makefile.common b/tools/libs/manage/Makefile= .common new file mode 100644 index 0000000000..533ba30fba --- /dev/null +++ b/tools/libs/manage/Makefile.common @@ -0,0 +1,3 @@ +CFLAGS +=3D -D__XEN_TOOLS__ + +OBJS-y +=3D core.o diff --git a/tools/libs/manage/core.c b/tools/libs/manage/core.c new file mode 100644 index 0000000000..b5fa67b036 --- /dev/null +++ b/tools/libs/manage/core.c @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2024 SUSE Software Solutions Germany GmbH + * + * 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; + * version 2.1 of the License. + * + * 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 + +#include +#include +#include + +#include +#include +#include +#include + +#include +#include + +struct xenmanage_handle { + xentoollog_logger *logger, *logger_tofree; + unsigned int flags; + xencall_handle *xcall; +}; + +xenmanage_handle *xenmanage_open(xentoollog_logger *logger, + unsigned open_flags) +{ + xenmanage_handle *hdl =3D calloc(1, sizeof(*hdl)); + int saved_errno; + + if ( !hdl ) + return NULL; + + if ( open_flags ) + { + errno =3D EINVAL; + goto err; + } + + hdl->flags =3D open_flags; + hdl->logger =3D logger; + hdl->logger_tofree =3D NULL; + + if ( !hdl->logger ) + { + hdl->logger =3D hdl->logger_tofree =3D + (xentoollog_logger *) + xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0); + if ( !hdl->logger ) + goto err; + } + + hdl->xcall =3D xencall_open(hdl->logger, 0); + if ( !hdl->xcall ) + goto err; + + return hdl; + +err: + saved_errno =3D errno; + xenmanage_close(hdl); + errno =3D saved_errno; + + return NULL; +} + +int xenmanage_close(xenmanage_handle *hdl) +{ + if ( !hdl ) + return 0; + + xencall_close(hdl->xcall); + xtl_logger_destroy(hdl->logger_tofree); + free(hdl); + return 0; +} + +static int xenmanage_do_domctl_get_domain_state(xenmanage_handle *hdl, + unsigned int domid_in, + unsigned int *domid_out, + unsigned int *state, + uint64_t *unique_id) +{ + struct xen_domctl *buf; + int saved_errno; + int ret; + + buf =3D xencall_alloc_buffer(hdl->xcall, sizeof(*buf)); + if ( !buf ) + { + errno =3D ENOMEM; + return -1; + } + + memset(buf, 0, sizeof(*buf)); + + buf->cmd =3D XEN_DOMCTL_get_domain_state; + buf->domain =3D domid_in; + + ret =3D xencall1(hdl->xcall, __HYPERVISOR_domctl, (unsigned long)buf); + saved_errno =3D errno; + if ( !ret ) + { + struct xen_domctl_get_domain_state *st =3D &buf->u.get_domain_stat= e; + + if ( domid_out ) + *domid_out =3D buf->domain; + if ( state ) + { + *state =3D 0; + if ( st->state & XEN_DOMCTL_GETDOMSTATE_STATE_EXIST ) + *state |=3D XENMANAGE_GETDOMSTATE_STATE_EXIST; + if ( st->state & XEN_DOMCTL_GETDOMSTATE_STATE_SHUTDOWN ) + *state |=3D XENMANAGE_GETDOMSTATE_STATE_SHUTDOWN; + if ( st->state & XEN_DOMCTL_GETDOMSTATE_STATE_DYING ) + *state |=3D XENMANAGE_GETDOMSTATE_STATE_DYING; + if ( st->state & XEN_DOMCTL_GETDOMSTATE_STATE_DEAD ) + *state |=3D XENMANAGE_GETDOMSTATE_STATE_DEAD; + } + if ( unique_id ) + *unique_id =3D st->unique_id; + } + + xencall_free_buffer(hdl->xcall, buf); + + errno =3D saved_errno; + + return ret; +} + +int xenmanage_get_domain_info(xenmanage_handle *hdl, unsigned int domid, + unsigned int *state, uint64_t *unique_id) +{ + if ( !hdl || domid >=3D DOMID_FIRST_RESERVED ) + { + errno =3D EINVAL; + return -1; + } + + return xenmanage_do_domctl_get_domain_state(hdl, domid, NULL, state, + unique_id); +} + +int xenmanage_poll_changed_domain(xenmanage_handle *hdl, unsigned int *dom= id, + unsigned int *state, uint64_t *unique_id) +{ + if ( !hdl || !domid ) + { + errno =3D EINVAL; + return -1; + } + + return xenmanage_do_domctl_get_domain_state(hdl, DOMID_INVALID, domid, + state, unique_id); +} diff --git a/tools/libs/manage/libxenmanage.map b/tools/libs/manage/libxenm= anage.map new file mode 100644 index 0000000000..64c793e603 --- /dev/null +++ b/tools/libs/manage/libxenmanage.map @@ -0,0 +1,8 @@ +VERS_1.0 { + global: + xenmanage_open; + xenmanage_close; + xenmanage_get_domain_info; + xenmanage_poll_changed_domain; + local: *; /* Do not expose anything by default */ +}; diff --git a/tools/libs/uselibs.mk b/tools/libs/uselibs.mk index 7aa8d83e06..c0a234cfec 100644 --- a/tools/libs/uselibs.mk +++ b/tools/libs/uselibs.mk @@ -16,6 +16,8 @@ LIBS_LIBS +=3D devicemodel USELIBS_devicemodel :=3D toollog toolcore call LIBS_LIBS +=3D hypfs USELIBS_hypfs :=3D toollog toolcore call +LIBS_LIBS +=3D manage +USELIBS_manage :=3D toollog toolcore call LIBS_LIBS +=3D ctrl USELIBS_ctrl :=3D toollog call evtchn gnttab foreignmemory devicemodel LIBS_LIBS +=3D guest --=20 2.43.0