rng.c 3.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * QEMU Random Number Generator Backend
 *
 * Copyright IBM, Corp. 2012
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

13
#include "qemu/osdep.h"
14
#include "sysemu/rng.h"
15
#include "qapi/error.h"
16
#include "qapi/qmp/qerror.h"
17
#include "qom/object_interfaces.h"
18 19 20 21 22 23

void rng_backend_request_entropy(RngBackend *s, size_t size,
                                 EntropyReceiveFunc *receive_entropy,
                                 void *opaque)
{
    RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
24
    RngRequest *req;
25 26

    if (k->request_entropy) {
27 28 29 30 31 32 33 34 35 36
        req = g_malloc(sizeof(*req));

        req->offset = 0;
        req->size = size;
        req->receive_entropy = receive_entropy;
        req->opaque = opaque;
        req->data = g_malloc(req->size);

        k->request_entropy(s, req);

37
        QSIMPLEQ_INSERT_TAIL(&s->requests, req, next);
38 39 40 41 42 43 44 45 46 47
    }
}

static bool rng_backend_prop_get_opened(Object *obj, Error **errp)
{
    RngBackend *s = RNG_BACKEND(obj);

    return s->opened;
}

48
static void rng_backend_complete(UserCreatable *uc, Error **errp)
49
{
50
    object_property_set_bool(OBJECT(uc), true, "opened", errp);
51 52 53 54 55 56
}

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);
57
    Error *local_err = NULL;
58 59 60 61 62 63

    if (value == s->opened) {
        return;
    }

    if (!value && s->opened) {
64
        error_setg(errp, QERR_PERMISSION_DENIED);
65 66 67 68
        return;
    }

    if (k->opened) {
69 70 71 72 73
        k->opened(s, &local_err);
        if (local_err) {
            error_propagate(errp, local_err);
            return;
        }
74 75
    }

76
    s->opened = true;
77 78
}

79 80 81 82 83 84 85 86
static void rng_backend_free_request(RngRequest *req)
{
    g_free(req->data);
    g_free(req);
}

static void rng_backend_free_requests(RngBackend *s)
{
87
    RngRequest *req, *next;
88

89 90
    QSIMPLEQ_FOREACH_SAFE(req, &s->requests, next, next) {
        rng_backend_free_request(req);
91 92
    }

93
    QSIMPLEQ_INIT(&s->requests);
94 95 96 97
}

void rng_backend_finalize_request(RngBackend *s, RngRequest *req)
{
98
    QSIMPLEQ_REMOVE(&s->requests, req, RngRequest, next);
99 100 101
    rng_backend_free_request(req);
}

102 103
static void rng_backend_init(Object *obj)
{
104 105 106 107
    RngBackend *s = RNG_BACKEND(obj);

    QSIMPLEQ_INIT(&s->requests);

108 109 110 111 112 113
    object_property_add_bool(obj, "opened",
                             rng_backend_prop_get_opened,
                             rng_backend_prop_set_opened,
                             NULL);
}

114 115 116 117 118 119 120
static void rng_backend_finalize(Object *obj)
{
    RngBackend *s = RNG_BACKEND(obj);

    rng_backend_free_requests(s);
}

121 122 123 124 125 126 127
static void rng_backend_class_init(ObjectClass *oc, void *data)
{
    UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);

    ucc->complete = rng_backend_complete;
}

128
static const TypeInfo rng_backend_info = {
129 130 131 132
    .name = TYPE_RNG_BACKEND,
    .parent = TYPE_OBJECT,
    .instance_size = sizeof(RngBackend),
    .instance_init = rng_backend_init,
133
    .instance_finalize = rng_backend_finalize,
134
    .class_size = sizeof(RngBackendClass),
135
    .class_init = rng_backend_class_init,
136
    .abstract = true,
137 138 139 140
    .interfaces = (InterfaceInfo[]) {
        { TYPE_USER_CREATABLE },
        { }
    }
141 142 143 144 145 146 147 148
};

static void register_types(void)
{
    type_register_static(&rng_backend_info);
}

type_init(register_types);