From patchwork Tue Oct 30 23:02:53 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 195650 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 7C8422C0085 for ; Wed, 31 Oct 2012 10:04:02 +1100 (EST) Received: from localhost ([::1]:37426 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TTKqm-0005Zf-J3 for incoming@patchwork.ozlabs.org; Tue, 30 Oct 2012 19:04:00 -0400 Received: from eggs.gnu.org ([208.118.235.92]:49349) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TTKqN-0005M2-4W for qemu-devel@nongnu.org; Tue, 30 Oct 2012 19:03:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TTKqL-0004ff-KE for qemu-devel@nongnu.org; Tue, 30 Oct 2012 19:03:35 -0400 Received: from e28smtp02.in.ibm.com ([122.248.162.2]:44489) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TTKqK-0004eC-JO for qemu-devel@nongnu.org; Tue, 30 Oct 2012 19:03:33 -0400 Received: from /spool/local by e28smtp02.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 31 Oct 2012 04:33:29 +0530 Received: from d28relay01.in.ibm.com (9.184.220.58) by e28smtp02.in.ibm.com (192.168.1.132) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 31 Oct 2012 04:33:16 +0530 Received: from d28av04.in.ibm.com (d28av04.in.ibm.com [9.184.220.66]) by d28relay01.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q9UN3FrL23134212 for ; Wed, 31 Oct 2012 04:33:15 +0530 Received: from d28av04.in.ibm.com (loopback [127.0.0.1]) by d28av04.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q9V4X6x6003452 for ; Wed, 31 Oct 2012 15:33:06 +1100 Received: from titi.austin.rr.com (sig-9-76-13-6.mts.ibm.com [9.76.13.6]) by d28av04.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q9V4Wqe4002700; Wed, 31 Oct 2012 15:33:03 +1100 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Tue, 30 Oct 2012 18:02:53 -0500 Message-Id: <1351638178-15056-4-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.8.0 In-Reply-To: <1351638178-15056-1-git-send-email-aliguori@us.ibm.com> References: <1351638178-15056-1-git-send-email-aliguori@us.ibm.com> x-cbid: 12103023-5816-0000-0000-00000521DE2F X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 122.248.162.2 Cc: Amit Shah , Paolo Bonzini , Anthony Liguori , Andreas Faerber , "H. Peter Anvin" Subject: [Qemu-devel] [PATCH 3/8] rng: add RndBackend abstract object class X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This is the backend used by devices that need to request entropy. Signed-off-by: Anthony Liguori --- Makefile.objs | 2 ++ backends/Makefile.objs | 1 + backends/rng.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/qemu/rng.h | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 189 insertions(+) create mode 100644 backends/Makefile.objs create mode 100644 backends/rng.c create mode 100644 include/qemu/rng.h diff --git a/Makefile.objs b/Makefile.objs index 9eca179..1a14de6 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -101,6 +101,8 @@ common-obj-y += vl.o common-obj-$(CONFIG_SLIRP) += slirp/ +common-obj-y += backends/ + ###################################################################### # libseccomp ifeq ($(CONFIG_SECCOMP),y) diff --git a/backends/Makefile.objs b/backends/Makefile.objs new file mode 100644 index 0000000..06e08c7 --- /dev/null +++ b/backends/Makefile.objs @@ -0,0 +1 @@ +common-obj-y += rng.o diff --git a/backends/rng.c b/backends/rng.c new file mode 100644 index 0000000..06f2611 --- /dev/null +++ b/backends/rng.c @@ -0,0 +1,93 @@ +/* + * QEMU Random Number Generator Backend + * + * Copyright IBM, Corp. 2012 + * + * Authors: + * Anthony Liguori + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/rng.h" +#include "qerror.h" + +void rng_backend_request_entropy(RngBackend *s, size_t size, + EntropyReceiveFunc *receive_entropy, + void *opaque) +{ + RngBackendClass *k = RNG_BACKEND_GET_CLASS(s); + + if (k->request_entropy) { + k->request_entropy(s, size, receive_entropy, opaque); + } +} + +void rng_backend_cancel_requests(RngBackend *s) +{ + RngBackendClass *k = RNG_BACKEND_GET_CLASS(s); + + if (k->cancel_requests) { + k->cancel_requests(s); + } +} + +static bool rng_backend_prop_get_opened(Object *obj, Error **errp) +{ + RngBackend *s = RNG_BACKEND(obj); + + return s->opened; +} + +void rng_backend_open(RngBackend *s, Error **errp) +{ + object_property_set_bool(OBJECT(s), true, "opened", errp); +} + +static void rng_backend_prop_set_opened(Object *obj, bool value, Error **errp) +{ + RngBackend *s = RNG_BACKEND(obj); + RngBackendClass *k = RNG_BACKEND_GET_CLASS(s); + + if (value == s->opened) { + return; + } + + if (!value && s->opened) { + error_set(errp, QERR_PERMISSION_DENIED); + return; + } + + if (k->opened) { + k->opened(s, errp); + } + + if (!error_is_set(errp)) { + s->opened = value; + } +} + +static void rng_backend_init(Object *obj) +{ + object_property_add_bool(obj, "opened", + rng_backend_prop_get_opened, + rng_backend_prop_set_opened, + NULL); +} + +static TypeInfo rng_backend_info = { + .name = TYPE_RNG_BACKEND, + .parent = TYPE_OBJECT, + .instance_size = sizeof(RngBackend), + .instance_init = rng_backend_init, + .class_size = sizeof(RngBackendClass), + .abstract = true, +}; + +static void register_types(void) +{ + type_register_static(&rng_backend_info); +} + +type_init(register_types); diff --git a/include/qemu/rng.h b/include/qemu/rng.h new file mode 100644 index 0000000..7e9d672 --- /dev/null +++ b/include/qemu/rng.h @@ -0,0 +1,93 @@ +/* + * QEMU Random Number Generator Backend + * + * Copyright IBM, Corp. 2012 + * + * Authors: + * Anthony Liguori + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#ifndef QEMU_RNG_H +#define QEMU_RNG_H + +#include "qemu/object.h" +#include "qemu-common.h" +#include "error.h" + +#define TYPE_RNG_BACKEND "rng-backend" +#define RNG_BACKEND(obj) \ + OBJECT_CHECK(RngBackend, (obj), TYPE_RNG_BACKEND) +#define RNG_BACKEND_GET_CLASS(obj) \ + OBJECT_GET_CLASS(RngBackendClass, (obj), TYPE_RNG_BACKEND) +#define RNG_BACKEND_CLASS(klass) \ + OBJECT_CLASS_CHECK(RngBackendClass, (klass), TYPE_RNG_BACKEND) + +typedef struct RngBackendClass RngBackendClass; +typedef struct RngBackend RngBackend; + +typedef void (EntropyReceiveFunc)(void *opaque, + const void *data, + size_t size); + +struct RngBackendClass +{ + ObjectClass parent_class; + + void (*request_entropy)(RngBackend *s, size_t size, + EntropyReceiveFunc *recieve_entropy, void *opaque); + void (*cancel_requests)(RngBackend *s); + + void (*opened)(RngBackend *s, Error **errp); +}; + +struct RngBackend +{ + Object parent; + + /*< protected >*/ + bool opened; +}; + +/** + * rng_backend_request_entropy: + * @s: the backend to request entropy from + * @size: the number of bytes of data to request + * @receive_entropy: a function to be invoked when entropy is available + * @opaque: data that should be passed to @receive_entropy + * + * This function is used by the front-end to request entropy from an entropy + * source. This function can be called multiple times before @receive_entropy + * is invoked with different values of @receive_entropy and @opaque. The + * backend will queue each request and handle appropriate. + * + * The backend does not need to pass the full amount of data to @receive_entropy + * but will pass at a value greater than 0. + */ +void rng_backend_request_entropy(RngBackend *s, size_t size, + EntropyReceiveFunc *receive_entropy, + void *opaque); + +/** + * rng_backend_cancel_requests: + * @s: the backend to cancel all pending requests in + * + * Cancels all pending requests submitted by @rng_backend_request_entropy. This + * should be used by a device during reset or in preparation for live migration + * to stop tracking any request. + */ +void rng_backend_cancel_requests(RngBackend *s); + +/** + * rng_backend_open: + * @s: the backend to open + * @errp: a pointer to return the #Error object if an error occurs. + * + * This function will open the backend if it is not already open. Calling this + * function on an already opened backend will not result in an error. + */ +void rng_backend_open(RngBackend *s, Error **errp); + +#endif