From patchwork Tue Jun 27 10:03:20 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksandr Grytsov X-Patchwork-Id: 9811289 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 86899603F2 for ; Tue, 27 Jun 2017 10:06:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 78C312860C for ; Tue, 27 Jun 2017 10:06:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6D76F28111; Tue, 27 Jun 2017 10:06:03 +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 C856028111 for ; Tue, 27 Jun 2017 10:06:02 +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 1dPnLS-0007Gb-Tl; Tue, 27 Jun 2017 10:03:42 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dPnLR-0007Ey-3Z for xen-devel@lists.xenproject.org; Tue, 27 Jun 2017 10:03:41 +0000 Received: from [85.158.139.211] by server-9.bemta-5.messagelabs.com id 1C/96-01999-C7D22595; Tue, 27 Jun 2017 10:03:40 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrPIsWRWlGSWpSXmKPExsVyMfS6i261blC kwelvahbft0xmcmD0OPzhCksAYxRrZl5SfkUCa8b2JZYF61wren/cZ2tgPGrUxcjFISQwnVFi +71vzCAOi8BLFonzG/ewgDgSAv2sEgeeTgZyOICcJIlPX3W7GDmBzFKJ6Uc/s4PYQgLyEgdbL rNDTJrKJLG24y8rSIJNQEvi/u0VbCC2iICSxL1Vk5lA5jALpEjs/O0EEhYWcJaYfOkVE4jNIq Aq8Xn3fBYQm1fAQWLpxGYmiF1yEjfPdTKDtHIKOEpceaIOsdZB4mvDVNYJjAILGBlWMWoUpxa VpRbpGhrpJRVlpmeU5CZm5ugaGpjq5aYWFyemp+YkJhXrJefnbmIEBhUDEOxg7JvlfIhRkoNJ SZR3tURQpBBfUn5KZUZicUZ8UWlOavEhRg0ODoGfT/+2MEqx5OXnpSpJ8H7RBqoTLEpNT61Iy 8wBhj1MqQQHj5IIb9nxwEgh3uKCxNzizHSI1ClGY44rV9Z9YeKYcmD7FyYhsElS4rxeOkCTBE BKM0rz4AbB4vESo6yUMC8j0JlCPAWpRbmZJajyrxjFORiVhHkngtzDk5lXArfvFdApTECnsMw LADmlJBEhJdXAyHjbu25ig3tF6xO7X4mnAlctqVjRa5utW319Utvnlapbe86euxGqnXlf5/t+ 5YN2Gps2vUl9pvomKFK/gv+CzwR/0/Sv5YfD4lfqxHp2fKnf6hy8Y82JopvT8xtsOyPTIqp+q HIv18ur0lwuOO/Y6trmyDc/DZp7tpasertBuFtVmK/deYmKEktxRqKhFnNRcSIAwbrP2MICAA A= X-Env-Sender: al1img@gmail.com X-Msg-Ref: server-14.tower-206.messagelabs.com!1498557819!66429357!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.19; banners=-,-,- X-VirusChecked: Checked Received: (qmail 18238 invoked from network); 27 Jun 2017 10:03:39 -0000 Received: from mail-lf0-f68.google.com (HELO mail-lf0-f68.google.com) (209.85.215.68) by server-14.tower-206.messagelabs.com with AES128-GCM-SHA256 encrypted SMTP; 27 Jun 2017 10:03:39 -0000 Received: by mail-lf0-f68.google.com with SMTP id t72so2312560lff.0 for ; Tue, 27 Jun 2017 03:03:39 -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=zgdVvZa6qNaOBuEquC2eizCZuiWsUfN+LwOi2qqZm48=; b=m6PiWe5shKjm0vH3mkkc3vaxjehwQ16br1IwCLrMz7trGPVkHWp2wRf8VwJYM1ENuG 9UMOxAbP/iM7WLRS1oo4PByze/TnBcxnsXjkhALFwbGnWPHF72AsIaxhQQk9oUoc9yd5 JVUl8kvOUtPI7KUodiLqx4gN/EdfX/Iq9XTMwcOEoht7B18ujJny01tqeOpcD1rK/ZFS dVjtT322krRTsAllMWFbNIi2udBaYLlj3S18ha9MOKncFU2NEgx5J91a7urMh8uMotqt 5yJB6tPeI3ItgCMq+2WSTkEW5gGO+yUxPc8Be+5wT2JWrGne+LHitJ3fSGKJA8BUkaw9 qfDA== 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=zgdVvZa6qNaOBuEquC2eizCZuiWsUfN+LwOi2qqZm48=; b=VPWRlztHF3Bg5+3qqO7IFhaMN8/uLPLN3gLCijnsiy/eFkoGBXTbLq3DK+SXr23fp6 E4u3uKyiGZivH9UtyU69FgKdMizww/MkE4qbWB93huaPdFvckA1Y2GxcjVV+xIX16k7r 7M2J6aQ0WfClxQJHSk0Snr0HY4SmC/YExQBl5dXoOu65+suKPa6Kox+PRzt5fn0PgeQF E6Eg81o5mn5a0nhfI9+slo4clAaR8cS/cEdS0msNspsByAKpgnG1oeqji5dyD6Nu3WNk kL381mwft7yxaDnSXrZ9xDqHNl7LPK1Z+Zj8LLKvb7MEWgOF1mXeszU/1MtyuJ3SgSbZ X10Q== X-Gm-Message-State: AKS2vOxZ57chn0hol3FnKwAFqAXaZPdmQ043uPBnFb6liFWCEmzhz6Fj N+jMtw/ItU13P0xZAYw= X-Received: by 10.46.22.5 with SMTP id w5mr1193276ljd.26.1498557818637; Tue, 27 Jun 2017 03:03:38 -0700 (PDT) Received: from al1-pc.kyiv.epam.com (ll-74.141.223.85.sovam.net.ua. [85.223.141.74]) by smtp.gmail.com with ESMTPSA id r82sm751763lfr.57.2017.06.27.03.03.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 27 Jun 2017 03:03:38 -0700 (PDT) From: Oleksandr Grytsov To: xen-devel@lists.xenproject.org Date: Tue, 27 Jun 2017 13:03:20 +0300 Message-Id: <1498557807-10810-5-git-send-email-al1img@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1498557807-10810-1-git-send-email-al1img@gmail.com> References: <1498557807-10810-1-git-send-email-al1img@gmail.com> Cc: wei.liu2@citrix.com, ian.jackson@eu.citrix.com, Oleksandr Grytsov Subject: [Xen-devel] [PATCH v3 04/11] libxl: add generic function to add device 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_add functio. Almost all devices have similar libxl__device_xxxx_add function. This generic function implements same functionality but using the device handling framework. The device specific part this is setting xen store configuration. This part is moved to set_xenstore_config callback of the device framework. Signed-off-by: Oleksandr Grytsov --- tools/libxl/libxl_create.c | 2 + tools/libxl/libxl_device.c | 103 +++++++++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_internal.h | 15 +++++++ tools/libxl/libxl_usb.c | 2 + tools/libxl/libxl_vdispl.c | 17 ++++++- 5 files changed, 138 insertions(+), 1 deletion(-) diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c index bffbc45..4e5ba29 100644 --- a/tools/libxl/libxl_create.c +++ b/tools/libxl/libxl_create.c @@ -1430,6 +1430,8 @@ out: #define libxl_device_dtdev_list NULL #define libxl_device_dtdev_compare NULL +#define libxl__device_from_dtdev NULL +#define libxl__device_dtdev_setdefault NULL static DEFINE_DEVICE_TYPE_STRUCT(dtdev); const struct libxl_device_type *device_type_tbl[] = { diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c index 8bcfa2b..e202503 100644 --- a/tools/libxl/libxl_device.c +++ b/tools/libxl/libxl_device.c @@ -1793,6 +1793,109 @@ out: return AO_CREATE_FAIL(rc); } +static int device_add_domain_config(libxl__gc *gc, uint32_t domid, + const struct libxl_device_type *dt, + void *type) +{ + int rc; + libxl_domain_config d_config; + libxl__domain_userdata_lock *lock = NULL; + int *num_dev; + int i; + void *item = NULL; + + libxl_domain_config_init(&d_config); + + lock = libxl__lock_domain_userdata(gc, domid); + if (!lock) { + rc = ERROR_LOCK_FAIL; goto out; + } + + rc = libxl__get_domain_configuration(gc, domid, &d_config); + if (rc) goto out; + + num_dev = libxl__device_type_get_num(dt, &d_config); + + /* Check for existing device */ + for (i = 0; i < *num_dev; i++) { + if (dt->compare(libxl__device_type_get_elem(dt, &d_config, i), type)) { + item = libxl__device_type_get_elem(dt, &d_config, i); + } + } + + if (!item) { + void **devs= libxl__device_type_get_ptr(dt, &d_config); + *devs = libxl__realloc(NOGC, *devs, + dt->dev_elem_size * (*num_dev + 1)); + item = libxl__device_type_get_elem(dt, &d_config, *num_dev); + (*num_dev)++; + } else { + dt->dispose(item); + } + + dt->init(item); + dt->copy(CTX, item, type); + + rc = libxl__dm_check_start(gc, &d_config, domid); + if (rc) goto out; + + rc = libxl__set_domain_configuration(gc, domid, &d_config); + if (rc) goto out; + + rc = 0; + +out: + if (lock) libxl__unlock_domain_userdata(lock); + libxl_domain_config_dispose(&d_config); + return rc; +} + +void libxl__device_add(libxl__egc *egc, uint32_t domid, + const struct libxl_device_type *dt, void *type, + libxl__ao_device *aodev) +{ + STATE_AO_GC(aodev->ao); + libxl__device *device; + int rc; + + rc = dt->set_default(gc, domid, type); + if (rc) goto out; + + GCNEW(device); + rc = dt->to_device(gc, domid, type, device); + if ( rc != 0 ) goto out; + + rc = libxl__device_exists(gc, XBT_NULL, device); + if (rc < 0) goto out; + if (rc == 1) { /* already exists in xenstore */ + LOGD(ERROR, domid, "device already exists in xenstore"); + aodev->action = LIBXL__DEVICE_ACTION_ADD; /* for error message */ + rc = ERROR_DEVICE_EXISTS; + goto out; + } + + if (aodev->update_json) { + rc = device_add_domain_config(gc, domid, dt, type); + if (rc) goto out; + } + + if (dt->set_xenstore_config) { + rc = dt->set_xenstore_config(gc, domid, type); + if (rc) goto out; + } + + aodev->dev = device; + aodev->action = LIBXL__DEVICE_ACTION_ADD; + libxl__wait_device_connection(egc, aodev); + + rc = 0; + +out: + aodev->rc = rc; + if(rc) aodev->callback(egc, aodev); + return; +} + void* libxl__device_list(const struct libxl_device_type *dt, libxl_ctx *ctx, uint32_t domid, int *num) { diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index c192559..3397655 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -3484,13 +3484,17 @@ struct libxl_device_type { void (*add)(libxl__egc *, libxl__ao *, uint32_t, libxl_domain_config *, libxl__multidev *); void *(*list)(libxl_ctx *, uint32_t, int *); + int (*set_default)(libxl__gc *, uint32_t, void *); + int (*to_device)(libxl__gc *, uint32_t, void *, libxl__device *); void (*init)(void *); + void (*copy)(libxl_ctx *, void *, void *); void (*dispose)(void *); int (*compare)(void *, void *); void (*merge)(libxl_ctx *, void *, void *); int (*dm_needed)(void *, unsigned); void (*update_config)(libxl__gc *, void *, void *); int (*from_xenstore)(libxl__gc *, const char *, uint32_t, void *); + int (*set_xenstore_config)(libxl__gc *, uint32_t, void *); }; #define DEFINE_DEVICE_TYPE_STRUCT_X(name, sname, ...) \ @@ -3502,7 +3506,14 @@ struct libxl_device_type { .add = libxl__add_ ## name ## s, \ .list = (void *(*)(libxl_ctx *, uint32_t, int *)) \ libxl_device_ ## sname ## _list, \ + .set_default = (int (*)(libxl__gc *, uint32_t, void *)) \ + libxl__device_ ## sname ## _setdefault, \ + .to_device = (int (*)(libxl__gc *, uint32_t, \ + void *, libxl__device *)) \ + libxl__device_from_ ## name, \ .init = (void (*)(void *))libxl_device_ ## sname ## _init, \ + .copy = (void (*)(libxl_ctx *, void *, void *)) \ + libxl_device_ ## sname ## _copy, \ .dispose = (void (*)(void *))libxl_device_ ## sname ## _dispose, \ .compare = (int (*)(void *, void *)) \ libxl_device_ ## sname ## _compare, \ @@ -4354,6 +4365,10 @@ static inline bool libxl__acpi_defbool_val(const libxl_domain_build_info *b_info return libxl_defbool_val(b_info->acpi) && libxl_defbool_val(b_info->u.hvm.acpi); } + +void libxl__device_add(libxl__egc *egc, uint32_t domid, + const struct libxl_device_type *dt, void *type, + libxl__ao_device *aodev); void* libxl__device_list(const struct libxl_device_type *dt, libxl_ctx *ctx, uint32_t domid, int *num); void libxl__device_list_free(const struct libxl_device_type *dt, diff --git a/tools/libxl/libxl_usb.c b/tools/libxl/libxl_usb.c index d8948d5..d1ec28f 100644 --- a/tools/libxl/libxl_usb.c +++ b/tools/libxl/libxl_usb.c @@ -1968,6 +1968,8 @@ void libxl_device_usbdev_list_free(libxl_device_usbdev *list, int nr) DEFINE_DEVICE_TYPE_STRUCT(usbctrl, .dm_needed = libxl_device_usbctrl_dm_needed ); + +#define libxl__device_from_usbdev NULL DEFINE_DEVICE_TYPE_STRUCT(usbdev); /* diff --git a/tools/libxl/libxl_vdispl.c b/tools/libxl/libxl_vdispl.c index a628adc..c79bcda 100644 --- a/tools/libxl/libxl_vdispl.c +++ b/tools/libxl/libxl_vdispl.c @@ -14,6 +14,21 @@ #include "libxl_internal.h" +static int libxl__device_vdispl_setdefault(libxl__gc *gc, uint32_t domid, + libxl_device_vdispl *vdispl) +{ + int rc; + + rc = libxl__resolve_domid(gc, vdispl->backend_domname, + &vdispl->backend_domid); + + if (vdispl->devid == -1) { + vdispl->devid = libxl__device_nextid(gc, domid, "vdispl"); + } + + return rc; +} + static int libxl__device_from_vdispl(libxl__gc *gc, uint32_t domid, libxl_device_vdispl *vdispl, libxl__device *device) @@ -47,7 +62,7 @@ static void libxl__device_vdispl_add(libxl__egc *egc, uint32_t domid, libxl_device_vdispl *vdispl, libxl__ao_device *aodev) { - + libxl__device_add(egc, domid, &libxl__vdispl_devtype, vdispl, aodev); } libxl_device_vdispl *libxl_device_vdispl_list(libxl_ctx *ctx, uint32_t domid,