From patchwork Tue Jul 18 14:25:19 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksandr Grytsov X-Patchwork-Id: 9848513 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 87908602C8 for ; Tue, 18 Jul 2017 14:27:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7A9C926E74 for ; Tue, 18 Jul 2017 14:27:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6F45228577; Tue, 18 Jul 2017 14:27:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.6 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D5D6026E74 for ; Tue, 18 Jul 2017 14:27:48 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dXTRY-0002VO-Om; Tue, 18 Jul 2017 14:25:44 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dXTRX-0002Uh-0Q for xen-devel@lists.xenproject.org; Tue, 18 Jul 2017 14:25:43 +0000 Received: from [85.158.143.35] by server-9.bemta-6.messagelabs.com id 9D/96-03406-66A1E695; Tue, 18 Jul 2017 14:25:42 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrGIsWRWlGSWpSXmKPExsVyMfS6i26qVF6 kwedruhbft0xmcmD0OPzhCksAYxRrZl5SfkUCa8aJ61+ZC9qkK441tTM1MP4S7mLk4hASmM4o MX//GXYQh0XgJYvEqYVdLF2MnBwSAv2sEl0vkyHsJImm2TPZIOxyiSXHO5hAbCEBeYmDLZfZI SZNY5LoufwNrIhNQEvi/u0VYLaIgJLEvVWTwRqYBVIk9q5rYwSxhQWCJHbvvMkKYrMIqEosfP eAuYuRg4NXwEFi/iQliF1yEjfPdYKFOQUcJbbOKIdY6yBxo/UV+wRGgQWMDKsYNYpTi8pSi3S NTPSSijLTM0pyEzNzdA0NzPRyU4uLE9NTcxKTivWS83M3MQLDigEIdjDu+xh5iFGSg0lJlHf1 gdxIIb6k/JTKjMTijPii0pzU4kOMGhwcAj+f/m1hlGLJy89LVZLgXSWRFykkWJSanlqRlpkDD HyYUgkOHiUR3nyQNG9xQWJucWY6ROoUoyXHlSvrvjBxTDmwHUi+mvD/G5MQ2DwpcV4FSaAGAZ CGjNI8uHGw6LzEKCslzMsIdKwQT0FqUW5mCar8K0ZxDkYlYd5GkLU8mXklcFtfAR3EBHSQsG8 OyEEliQgpqQbG8rnRn29MClr76vLawMttbI4Wd2QLL6c9aW9iuVwe6/LzccDSSXvSPss9rMlZ znjJ20Zc0yO3fA8HU2hm0oHI5U8Ob1duZuroe+swrVfLeem886cmvX3Sbh+Z9DlBud5G4wxPc M/Zw++uL2SRnzMxtvTQp6gLahYb3wnWfd975P31up/VfyaJK7EUZyQaajEXFScCAAjctbLJAg AA X-Env-Sender: al1img@gmail.com X-Msg-Ref: server-2.tower-21.messagelabs.com!1500387941!59897028!1 X-Originating-IP: [209.85.215.68] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.25; banners=-,-,- X-VirusChecked: Checked Received: (qmail 32707 invoked from network); 18 Jul 2017 14:25:41 -0000 Received: from mail-lf0-f68.google.com (HELO mail-lf0-f68.google.com) (209.85.215.68) by server-2.tower-21.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 18 Jul 2017 14:25:41 -0000 Received: by mail-lf0-f68.google.com with SMTP id p11so1997273lfd.1 for ; Tue, 18 Jul 2017 07:25:41 -0700 (PDT) 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=SsZmW/54BVuREvdUsz3xqMNYXBEzm66v+rT+isYOjkE=; b=OjlAciXLivNIl2O8kSwoCtkw5L5b1UXWW4yYbF4ARXeIqB9fB6opGiaJqYnbwIs/al OiEUE7PbbA4lQEQNOWKFslGUe3bxgxDV/Irh3TCCL2aYOKaQSyMFZXl8R4Vc/cOBuILB CXez1tM9vwFLQgc5fpB7VsTDCnbHYTdurFGs0cHhDPwA3aVkz0Mg2pi7vrgwA6+QDJmb 4U9BswwcmSU7eSycYWREKOO7Mi5UgZSPfPSM1nPmxHDmLefV/0eCJv5pbM7eBYLlCfDf 38o1hFV0vS+Kah/O4xXy5HmY6Dug5zm5o8XBA4wli+RqEWqnzER4euD5lKdkpE4/YFxH ZcVQ== 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=SsZmW/54BVuREvdUsz3xqMNYXBEzm66v+rT+isYOjkE=; b=KuIM1BD5d7pezRSWO4/ieGfXuVbbFlPf6DIszvjD1ve+pS+lIJTVQRG6tafsrCiYTM g5LSkbfzhOcHRqHDOJP6xzXLrE4KprAYBrPCHMxAOF/F2N+Rh4MsBiqIIBif5IwIZeWj 2pppmhrI1udVD+LMAqNH49xqJ3IKCEzcsIExDjLDK775ggVynl8ZbspS59p9b5eVx7ru kruHyqbVWrFo7BoSsLTv3oD3cpwmGNqMqmMjTP9x/+WFnugoVpPUfs442e8xd7w1ALX4 LF/cVAj9eqKFnkU2rZQ8aX5uPwgOHofPJFi/WOeH1t0JSPFD/x7tg+JoWFP3y+30svrH kOPg== X-Gm-Message-State: AIVw110/j6lkg0XC7SjAenKgwwF6qnxvv86DhE9EjFu/ZUDED+M9V5DI MMmSIZIdgYLCfyjQUkk= X-Received: by 10.25.212.69 with SMTP id l66mr714548lfg.56.1500387940807; Tue, 18 Jul 2017 07:25:40 -0700 (PDT) Received: from al1-pc.kyiv.epam.com (ll-58.209.223.85.sovam.net.ua. [85.223.209.58]) by smtp.gmail.com with ESMTPSA id s134sm620442lfe.27.2017.07.18.07.25.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 18 Jul 2017 07:25:40 -0700 (PDT) From: Oleksandr Grytsov To: xen-devel@lists.xenproject.org Date: Tue, 18 Jul 2017 17:25:19 +0300 Message-Id: <1500387930-16317-3-git-send-email-al1img@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1500387930-16317-1-git-send-email-al1img@gmail.com> References: <1500387930-16317-1-git-send-email-al1img@gmail.com> Cc: ian.jackson@eu.citrix.com, wei.liu2@citrix.com, Oleksandr Grytsov Subject: [Xen-devel] [PATCH v4 02/13] libxl: add generic functions to get and free device list X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Oleksandr Grytsov Add libxl__device_list and libxl__device_list_free functions to handle device list using the device framework. Signed-off-by: Oleksandr Grytsov --- tools/libxl/libxl_device.c | 66 ++++++++++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_internal.h | 8 ++++++ 2 files changed, 74 insertions(+) diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c index 07165f0..f1d4848 100644 --- a/tools/libxl/libxl_device.c +++ b/tools/libxl/libxl_device.c @@ -1991,6 +1991,72 @@ out: return rc; } +void *libxl__device_list(libxl__gc *gc, const struct libxl_device_type *dt, + uint32_t domid, const char* name, int *num) +{ + void *r = NULL; + void *list = NULL; + void *item = NULL; + char *libxl_path; + char **dir = NULL; + unsigned int ndirs = 0; + int rc; + + *num = 0; + + libxl_path = GCSPRINTF("%s/device/%s", + libxl__xs_libxl_path(gc, domid), name); + + dir = libxl__xs_directory(gc, XBT_NULL, libxl_path, &ndirs); + + if (dir && ndirs) { + list = libxl__malloc(NOGC, dt->dev_elem_size * ndirs); + void *end = (uint8_t *)list + ndirs * dt->dev_elem_size; + item = list; + + while (item < end) { + dt->init(item); + + if (dt->from_xenstore) { + char* device_libxl_path = GCSPRINTF("%s/%s", libxl_path, *dir); + rc = dt->from_xenstore(gc, device_libxl_path, atoi(*dir), item); + if (rc) goto out; + } + + item = (uint8_t*)item + dt->dev_elem_size; + ++dir; + } + } + + *num = ndirs; + r = list; + list = NULL; + +out: + + if (list) { + *num = 0; + while(item >= list) { + dt->dispose(item); + item = (uint8_t*)item - dt->dev_elem_size; + } + free(list); + } + + return r; +} + +void libxl__device_list_free(const struct libxl_device_type *dt, + void *list, int num) +{ + int i; + + for (i = 0; i < num; i++) + dt->dispose((uint8_t*)list + i * dt->dev_elem_size); + + free(list); +} + /* * Local variables: * mode: C diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 075dfe3..271ac89 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -3506,6 +3506,7 @@ struct libxl_device_type { int (*dm_needed)(void *, unsigned); void (*update_config)(libxl__gc *, void *, void *); int (*update_devid)(libxl__gc *, uint32_t, void *); + int (*from_xenstore)(libxl__gc *, const char *, libxl_devid, void *); int (*set_xenstore_config)(libxl__gc *, uint32_t, void *, flexarray_t *, flexarray_t *, flexarray_t *); }; @@ -4386,6 +4387,13 @@ void libxl__device_add_async(libxl__egc *egc, uint32_t domid, int libxl__device_add(libxl__gc *gc, uint32_t domid, const struct libxl_device_type *dt, void *type); +/* Caller is responsible for freeing the memory by calling + * libxl__device_list_free + */ +void* libxl__device_list(libxl__gc *gc, const struct libxl_device_type *dt, + uint32_t domid, const char* name, int *num); +void libxl__device_list_free(const struct libxl_device_type *dt, + void *list, int num); #endif /*