summaryrefslogtreecommitdiffstats
path: root/vhost_user_console.c
blob: 2997b8d0f7fc378b01b6dd05738fcffd918bb0e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * Copyright (c) 2023-2024 Virtual Open Systems SAS.
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or
 * (at your option) any later version.  See the COPYING file in the
 * top-level directory.
 *
 */


#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <sys/param.h>

/* Project header files */
#include "vhost_user_console.h"

#ifdef DEBUG
#define DBG(...) printf("vhost-user-console: " __VA_ARGS__)
#else
#define DBG(...)
#endif /* DEBUG */

static const int user_feature_bits[] = {
    VIRTIO_F_VERSION_1,
    VIRTIO_RING_F_INDIRECT_DESC,
    VIRTIO_RING_F_EVENT_IDX,
    VIRTIO_F_RING_RESET,
    VIRTIO_F_NOTIFY_ON_EMPTY,
    VHOST_INVALID_FEATURE_BIT
};

static void vhost_user_console_get_config(VirtIODevice *vdev, uint8_t *config)
{
    VHostUserConsole *console = vdev->vhuconsole;

    DBG("vhost_user_console_get_config: Not yet implemented!\n");

    memcpy(config, &console->config, sizeof(struct virtio_console_config));
}

static void vhost_user_console_start(VirtIODevice *vdev)
{
    VHostUserConsole *vhuconsole = vdev->vhuconsole;
    VirtioBus *k = vdev->vbus;
    unsigned int i;
    int ret;

    DBG("vhost_user_console_start(...)\n");

    if (!k->set_guest_notifiers) {
        DBG("binding does not support guest notifiers\n");
        return;
    }

    ret = vhost_dev_enable_notifiers(vhuconsole->vhost_dev, vdev);
    if (ret < 0) {
        DBG("Error enabling host notifiers: %d\n", -ret);
        return;
    }

    ret = k->set_guest_notifiers(k->vdev, vhuconsole->vhost_dev->nvqs, true);
    if (ret < 0) {
        DBG("Error binding guest notifier: %d\n", -ret);
        goto err_host_notifiers;
    }

    vhuconsole->vhost_dev->acked_features = vdev->guest_features;

    ret = vhost_dev_start(vhuconsole->vhost_dev, vdev, true);
    if (ret < 0) {
        DBG("Error starting vhost: %d\n", -ret);
        goto err_guest_notifiers;
    }

    /*
     * guest_notifier_mask/pending not used yet, so just unmask
     * everything here.  virtio-pci will do the right thing by
     * enabling/disabling irqfd.
     */
    for (i = 0; i < vhuconsole->vhost_dev->nvqs; i++) {
        vhost_virtqueue_mask(vhuconsole->vhost_dev, vdev, i, false);
    }

    return;

err_guest_notifiers:
err_host_notifiers:
    DBG("vhu_start error\n");
    return;
}

static void vhost_user_console_stop(VirtIODevice *vdev)
{
    DBG("vhost_user_console_stop: not yet implemented\n");
    (void)vdev;
}

static void vhost_user_console_set_status(VirtIODevice *vdev, uint8_t status)
{
    VHostUserConsole *vhuconsole = vdev->vhuconsole;
    bool should_start = virtio_device_started(vdev, status);
    DBG("vhost_user_console_set_status: %d\n", status);

    if (vhuconsole->vhost_dev->started == should_start) {
        DBG("console->vhost_dev->started == should_start\n");
        return;
    }

    if (should_start) {
        vhost_user_console_start(vdev);
    } else {
        vhost_user_console_stop(vdev);
    }
}

static uint64_t vhost_user_console_get_features(VirtIODevice *vdev,
                                                uint64_t features)
{
    VHostUserConsole *s = vdev->vhuconsole;

    DBG("vhost_user_console_get_features()\n");

    return vhost_get_features(s->vhost_dev, user_feature_bits, features);
}

static void vhost_user_console_handle_output(VirtIODevice *vdev, VirtQueue *vq)
{
    /*
     * Not normally called; it's the daemon that handles the queue;
     * however virtio's cleanup path console call this.
     */
    DBG("vhost_user_console_handle_output: Not yet implemented!\n");
    (void)vdev;
    (void)vq;
}

static int vhost_user_console_config_change(struct vhost_dev *dev)
{
    VHostUserConsole *vhuconsole = dev->vdev->vhuconsole;
    DBG("vhost_user_console_console_config_change: Not yet implemented!\n");

    int ret = vhost_dev_get_config(dev, (uint8_t *)&vhuconsole->config,
                                   sizeof(struct virtio_console_config));
    if (ret < 0) {
        DBG("vhost_user_console_sound_config_change error\n");
        return -1;
    }

    virtio_notify_config(dev->vdev);

    return 0;
}

const VhostDevConfigOps console_config_ops = {
    .vhost_dev_config_notifier = vhost_user_console_config_change,
};

static void vhost_user_console_init(VirtIODevice *vdev);

void vhost_user_console_realize(int queue_num, int queue_size)
{
    VirtIODevice *vdev = global_vdev;
    int ret;

    DBG("vhost_user_console_device_realize\n");
    (void)queue_num;
    (void)queue_size;

    /* This needs to be added */
    proxy = (VirtIOMMIOProxy *)malloc(sizeof(VirtIOMMIOProxy));
    *proxy = (VirtIOMMIOProxy) {
        .legacy = 1,
    };

    /* VIRTIO_ID_CAN is 36, check virtio_ids.h in linux*/
    virtio_dev_init(vdev, "virtio-console", 3,
                    sizeof(vdev->vhuconsole->config));
    vhost_user_console_init(global_vdev);

    /* add queues */
    vdev->vhuconsole->rx_vq = virtio_add_queue(vdev, 64,
                                            vhost_user_console_handle_output);
    vdev->vhuconsole->tx_vq = virtio_add_queue(vdev, 64,
                                            vhost_user_console_handle_output);
    vdev->vhuconsole->ctrl_rx_vq = virtio_add_queue(vdev, 64,
                                            vhost_user_console_handle_output);
    vdev->vhuconsole->ctrl_tx_vq = virtio_add_queue(vdev, 64,
                                            vhost_user_console_handle_output);
    vdev->vhuconsole->vhost_dev->nvqs = 4;
    vdev->vhuconsole->num_queues = 4;
    vdev->vhuconsole->queue_size = 64;

    /* NOTE: global_vdev->vqs == vhuconsole->virtqs */
    vdev->vqs = (VirtQueue **)malloc(sizeof(VirtQueue *)
                                     * global_vdev->vhuconsole->num_queues);
    vdev->vqs[0] = vdev->vhuconsole->rx_vq;
    vdev->vqs[1] = vdev->vhuconsole->tx_vq;
    vdev->vqs[2] = vdev->vhuconsole->ctrl_rx_vq;
    vdev->vqs[3] = vdev->vhuconsole->ctrl_tx_vq;

    vdev->vhuconsole->vhost_vqs = (struct vhost_virtqueue *)malloc(
                                       sizeof(struct vhost_virtqueue) *
                                       vdev->vhuconsole->num_queues);

    /* Set up vhost device */
    vdev->vhuconsole->vhost_dev->num_queues = vdev->vhuconsole->num_queues;
    vdev->vhuconsole->vhost_dev->nvqs = vdev->vhuconsole->num_queues;
    vdev->vhuconsole->vhost_dev->vqs = vdev->vhuconsole->vhost_vqs;
    vdev->vhuconsole->vhost_dev->vq_index = 0;
    vdev->vhuconsole->vhost_dev->backend_features = 0;

    vhost_dev_set_config_notifier(vdev->vhuconsole->vhost_dev,
                                  &console_config_ops);

    /* TODO: Add error handling */
    vhost_dev_init(vdev->vhuconsole->vhost_dev);

    /* Pass the new obtained features */
    global_vdev->host_features = vdev->vhuconsole->vhost_dev->features;

    ret = vhost_dev_get_config(vdev->vhuconsole->vhost_dev,
                               (uint8_t *)&vdev->vhuconsole->config,
                               sizeof(struct virtio_console_config));
    if (ret < 0) {
        goto vhost_dev_init_failed;
    }

    vdev->vdev_class->print_config((uint8_t *)&vdev->vhuconsole->config);

    return;

vhost_dev_init_failed:
    DBG("vhost_dev_init_failed\n");
    return;
}

static void vhost_user_console_device_unrealize(VirtIODevice *vdev)
{
    DBG("vhost_user_blk_device_unrealize not yet implemented\n");
    (void)vdev;
}

/*
 * This function might be useful in the future:
 *
 * static struct vhost_dev *vhost_user_console_get_vhost(VirtIODevice *vdev)
 * {
 *     VHostUserConsole *vhuconsole = vdev->vhuconsole;
 *     return vhuconsole->vhost_dev;
 * }
 *
 */

static void print_config_console(uint8_t *config_data)
{
    struct virtio_console_config *config_strct =
        (struct virtio_console_config *)config_data;

    (void)config_strct;
    DBG("print_config_console:\n");
    DBG("\tuint16_t cols: %u\n", config_strct->cols);
    DBG("\tuint16_t rows: %u\n", config_strct->rows);
    DBG("\tuint16_t max_nr_ports: %u\n", config_strct->max_nr_ports);
    DBG("\tuint16_t emerg_wr: %u\n", config_strct->emerg_wr);
}

static void virtio_dev_class_init(VirtIODevice *vdev)
{
    DBG("virtio_dev_class_init\n");

    vdev->vdev_class = (VirtioDeviceClass *)malloc(sizeof(VirtioDeviceClass));
    vdev->vdev_class->parent = vdev;
    vdev->vdev_class->realize = vhost_user_console_realize;
    vdev->vdev_class->unrealize = vhost_user_console_device_unrealize;
    vdev->vdev_class->get_config = vhost_user_console_get_config;
    vdev->vdev_class->get_features = vhost_user_console_get_features;
    vdev->vdev_class->set_status = vhost_user_console_set_status;
    vdev->vdev_class->update_mem_table = update_mem_table;
    vdev->vdev_class->print_config = print_config_console;
}

static void vhost_user_console_init(VirtIODevice *vdev)
{

    DBG("vhost_user_console_init\n");

    VHostUserConsole *vhuconsole =
                        (VHostUserConsole *)malloc(sizeof(VHostUserConsole));
    vdev->vhuconsole = vhuconsole;
    vdev->nvqs = (int *)&vdev->vhdev->nvqs;
    vhuconsole->parent = vdev;
    vhuconsole->virtqs = vdev->vqs;
    vhuconsole->vhost_dev = vdev->vhdev;

    virtio_dev_class_init(vdev);
    virtio_loopback_bus_init(vdev->vbus);
}