rng-egd.c 4.1 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 15
#include "sysemu/rng.h"
#include "sysemu/char.h"
16
#include "qapi/error.h"
17
#include "qapi/qmp/qerror.h"
18 19 20 21 22 23 24 25

#define TYPE_RNG_EGD "rng-egd"
#define RNG_EGD(obj) OBJECT_CHECK(RngEgd, (obj), TYPE_RNG_EGD)

typedef struct RngEgd
{
    RngBackend parent;

26
    CharBackend chr;
27 28 29
    char *chr_name;
} RngEgd;

30
static void rng_egd_request_entropy(RngBackend *b, RngRequest *req)
31 32
{
    RngEgd *s = RNG_EGD(b);
33
    size_t size = req->size;
34 35 36 37 38 39 40 41 42

    while (size > 0) {
        uint8_t header[2];
        uint8_t len = MIN(size, 255);

        /* synchronous entropy request */
        header[0] = 0x02;
        header[1] = len;

43 44
        /* XXX this blocks entire thread. Rewrite to use
         * qemu_chr_fe_write and background I/O callbacks */
45
        qemu_chr_fe_write_all(&s->chr, header, sizeof(header));
46 47 48 49 50 51 52 53

        size -= len;
    }
}

static int rng_egd_chr_can_read(void *opaque)
{
    RngEgd *s = RNG_EGD(opaque);
54
    RngRequest *req;
55 56
    int size = 0;

57
    QSIMPLEQ_FOREACH(req, &s->parent.requests, next) {
58 59 60 61 62 63 64 65 66
        size += req->size - req->offset;
    }

    return size;
}

static void rng_egd_chr_read(void *opaque, const uint8_t *buf, int size)
{
    RngEgd *s = RNG_EGD(opaque);
67
    size_t buf_offset = 0;
68

69 70
    while (size > 0 && !QSIMPLEQ_EMPTY(&s->parent.requests)) {
        RngRequest *req = QSIMPLEQ_FIRST(&s->parent.requests);
71 72
        int len = MIN(size, req->size - req->offset);

73 74
        memcpy(req->data + req->offset, buf + buf_offset, len);
        buf_offset += len;
75 76 77 78 79 80
        req->offset += len;
        size -= len;

        if (req->offset == req->size) {
            req->receive_entropy(req->opaque, req->data, req->size);

81
            rng_backend_finalize_request(&s->parent, req);
82 83 84 85 86 87 88
        }
    }
}

static void rng_egd_opened(RngBackend *b, Error **errp)
{
    RngEgd *s = RNG_EGD(b);
89
    Chardev *chr;
90 91

    if (s->chr_name == NULL) {
92 93
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
                   "chardev", "a valid character device");
94 95 96
        return;
    }

97 98
    chr = qemu_chr_find(s->chr_name);
    if (chr == NULL) {
99 100
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
                  "Device '%s' not found", s->chr_name);
101 102
        return;
    }
103 104 105
    if (!qemu_chr_fe_init(&s->chr, chr, errp)) {
        return;
    }
106

107
    /* FIXME we should resubmit pending requests when the CDS reconnects. */
108
    qemu_chr_fe_set_handlers(&s->chr, rng_egd_chr_can_read,
109
                             rng_egd_chr_read, NULL, s, NULL, true);
110 111 112 113 114 115 116 117
}

static void rng_egd_set_chardev(Object *obj, const char *value, Error **errp)
{
    RngBackend *b = RNG_BACKEND(obj);
    RngEgd *s = RNG_EGD(b);

    if (b->opened) {
118
        error_setg(errp, QERR_PERMISSION_DENIED);
119
    } else {
120
        g_free(s->chr_name);
121 122 123 124 125 126 127
        s->chr_name = g_strdup(value);
    }
}

static char *rng_egd_get_chardev(Object *obj, Error **errp)
{
    RngEgd *s = RNG_EGD(obj);
128
    Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
129

130 131
    if (chr && chr->label) {
        return g_strdup(chr->label);
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    }

    return NULL;
}

static void rng_egd_init(Object *obj)
{
    object_property_add_str(obj, "chardev",
                            rng_egd_get_chardev, rng_egd_set_chardev,
                            NULL);
}

static void rng_egd_finalize(Object *obj)
{
    RngEgd *s = RNG_EGD(obj);

148
    qemu_chr_fe_deinit(&s->chr);
149 150 151 152 153 154 155 156 157 158 159
    g_free(s->chr_name);
}

static void rng_egd_class_init(ObjectClass *klass, void *data)
{
    RngBackendClass *rbc = RNG_BACKEND_CLASS(klass);

    rbc->request_entropy = rng_egd_request_entropy;
    rbc->opened = rng_egd_opened;
}

160
static const TypeInfo rng_egd_info = {
161 162 163 164 165 166 167 168 169 170 171 172 173 174
    .name = TYPE_RNG_EGD,
    .parent = TYPE_RNG_BACKEND,
    .instance_size = sizeof(RngEgd),
    .class_init = rng_egd_class_init,
    .instance_init = rng_egd_init,
    .instance_finalize = rng_egd_finalize,
};

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

type_init(register_types);