aboutsummaryrefslogtreecommitdiffstats
path: root/vhost_loopback.c
blob: 1855ee87b1068dfb296ddf07f6591185c79aef41 (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
 * Based on vhost.c of QEMU project;
 *
 *   Copyright Red Hat, Inc. 2010
 *
 *   Authors:
 *    Michael S. Tsirkin <mst@redhat.com>
 *
 *   Copyright Red Hat, Inc. 2010
 *
 *   Authors:
 *    Michael S. Tsirkin <mst@redhat.com>
 *
 * Copyright 2022-2023 Virtual Open Systems SAS.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sys/eventfd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <pthread.h>
#include <stdbool.h>
#include <sys/param.h>
#include <assert.h>

/* For socket */
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

/* Project header files */
#include "virtio_loopback.h"
#include "vhost_user_loopback.h"
#include "event_notifier.h"

/* vhost headers */
#include "vhost_loopback.h"

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

/*
 * Stop processing guest IO notifications in qemu.
 * Start processing them in vhost in kernel.
 */
int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
{
    int i, r, e;

    /*
     * We will pass the notifiers to the kernel, make sure that QEMU
     * doesn't interfere.
     */

    /* TODO: Check if this is still useful */
    r = virtio_device_grab_ioeventfd(vdev);
    if (r < 0) {
        DBG("binding does not support host notifiers\n");
        goto fail;
    }


    for (i = 0; i < hdev->nvqs; ++i) {
        r = virtio_bus_set_host_notifier(vdev->vbus, hdev->vq_index + i,
                                         true);
        if (r < 0) {
            DBG("vhost VQ %d notifier binding failed: %d", i, r);
            goto fail;
        }
    }

    return 0;

fail:
    DBG("Fail vhost_dev_enable_notifiers\n");
    return r;
}

/* TODO: This needs to be checked if it's still needed */
static int vhost_dev_has_iommu(struct vhost_dev *dev)
{
    VirtIODevice *vdev = dev->vdev;

    /*
     * For vhost, VIRTIO_F_IOMMU_PLATFORM means the backend support
     * incremental memory mapping API via IOTLB API. For platform that
     * does not have IOMMU, there's no need to enable this feature
     * which may cause unnecessary IOTLB miss/update transactions.
     */
    return virtio_bus_device_iommu_enabled(vdev) &&
           virtio_has_feature(vdev->host_features, VIRTIO_F_IOMMU_PLATFORM);
}

static int vhost_dev_set_features(struct vhost_dev *dev,
                                  bool enable_log)
{
    uint64_t features = dev->acked_features;
    int r;

    if (enable_log) {
        features |= 0x1ULL << VHOST_F_LOG_ALL;
    }

    /* TODO: check if this is needed */
    if (!vhost_dev_has_iommu(dev)) {
        features &= ~(0x1ULL << VIRTIO_F_IOMMU_PLATFORM);
    }

    r = vhost_user_set_features(dev, features);
    if (r < 0) {
        DBG("vhost_set_features failed\n");
        goto out;
    }

out:
    return r;
}

static int vhost_virtqueue_set_addr(struct vhost_dev *dev,
                                    struct vhost_virtqueue *vq,
                                    unsigned idx, bool enable_log)
{
    struct vhost_vring_addr addr;
    int r;

    memset(&addr, 0, sizeof(struct vhost_vring_addr));

    addr.desc_user_addr = (uint64_t)(unsigned long)vq->desc_phys;
    addr.avail_user_addr = (uint64_t)(unsigned long)vq->avail_phys;
    addr.used_user_addr = (uint64_t)(unsigned long)vq->used_phys;

    DBG("Print physical addresses of vrings:\n");
    DBG("\tvq->desc_phys: 0x%llx\n", vq->desc_phys);
    DBG("\tvq->avail_phys: 0x%llx\n", vq->avail_phys);
    DBG("\tvq->used_phys: 0x%llx\n", vq->used_phys);

    addr.index = idx;
    addr.log_guest_addr = vq->used_phys;
    addr.flags = enable_log ? (1 << VHOST_VRING_F_LOG) : 0;

    r = vhost_user_set_vring_addr(dev, &addr);
    if (r < 0) {
        DBG("vhost_set_vring_addr failed\n");
    }
    return r;
}

uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
                            uint64_t features)
{
    const int *bit = feature_bits;
    while (*bit != VHOST_INVALID_FEATURE_BIT) {
        uint64_t bit_mask = (1ULL << *bit);
        if (!(hdev->features & bit_mask)) {
            features &= ~bit_mask;
        }
        bit++;
    }
    return features;
}

void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
                        uint64_t features)
{
    const int *bit = feature_bits;
    while (*bit != VHOST_INVALID_FEATURE_BIT) {
        uint64_t bit_mask = (1ULL << *bit);
        if (features & bit_mask) {
            hdev->acked_features |= bit_mask;
        }
        bit++;
    }
}



/* Mask/unmask events from this vq. */
void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
                         bool mask)
{
    struct VirtQueue *vvq = virtio_get_queue(vdev, n);
    int r, index = n - hdev->vq_index;
    struct vhost_vring_file file;

    if (mask) {
        file.fd = event_notifier_get_wfd(&hdev->vqs[index].masked_notifier);
    } else {
        file.fd = event_notifier_get_wfd(virtio_queue_get_guest_notifier(vvq));
    }

    file.index = vhost_user_get_vq_index(hdev, n);

    r = vhost_user_set_vring_call(&file);
    if (r < 0) {
        DBG("vhost_set_vring_call failed\n");
    }
}

static int vhost_virtqueue_start(struct vhost_dev *dev,
                                struct VirtIODevice *vdev,
                                struct vhost_virtqueue *vq,
                                unsigned idx)
{
    VirtioBus *vbus = vdev->vbus;
    uint64_t s, l, a;
    int r;

    int vhost_vq_index = vhost_user_get_vq_index(dev, idx);
    struct vhost_vring_file file = {
        .index = vhost_vq_index
    };
    struct vhost_vring_state state = {
        .index = vhost_vq_index
    };
    struct VirtQueue *vvq = virtio_get_queue(vdev, idx);

    a = virtio_queue_get_desc_addr(vdev, idx);
    if (a == 0) {
        /* Queue might not be ready for start */
        DBG("Error: Queue might not be ready for start\n");
        return 0;
    }

    vq->num = state.num = virtio_queue_get_num(vdev, idx);

    r = vhost_user_set_vring_num(dev, &state);
    if (r) {
        DBG("vhost_set_vring_num failed\n");
        return r;
    }

    state.num = virtio_queue_get_last_avail_idx(vdev, idx);
    r = vhost_user_set_vring_base(dev, &state);
    if (r) {
        DBG("vhost_set_vring_base failed\n");
        return r;
    }

    vq->desc_size = s = l = virtio_queue_get_desc_size(vdev, idx);
    vq->desc_phys = vring_phys_addrs[idx] << 12;
    vq->desc = (void *)virtio_queue_get_desc_addr(vdev, idx);
    if (!vq->desc || l != s) {
        DBG("Error : vq->desc = a\n");
        r = -ENOMEM;
        return r;
    }

    vq->avail_size = s = l = virtio_queue_get_avail_size(vdev, idx);
    vq->avail_phys = vq->desc_phys + virtio_queue_get_avail_addr(vdev, idx)
                                   - virtio_queue_get_desc_addr(vdev, idx);
    vq->avail = (void *)virtio_queue_get_avail_addr(vdev, idx);
    if (!vq->avail || l != s) {
        DBG("Error : vq->avail = a\n");
        r = -ENOMEM;
        return r;
    }

    vq->used_size = s = l = virtio_queue_get_used_size(vdev, idx);
    vq->used_phys = a = vq->avail_phys + virtio_queue_get_used_addr(vdev, idx)
                                       - virtio_queue_get_avail_addr(vdev, idx);
    vq->used = (void *)virtio_queue_get_used_addr(vdev, idx);
    if (!vq->used || l != s) {
        DBG("Error : vq->used = a\n");
        r = -ENOMEM;
        return r;
    }

    r = vhost_virtqueue_set_addr(dev, vq, vhost_vq_index, dev->log_enabled);
    if (r < 0) {
        DBG("Fail vhost_virtqueue_set_addr\n");
        return r;
    }

    /* The next line has to be disable for rng */
    /* Clear and discard previous events if any. */
    event_notifier_test_and_clear(virtio_queue_get_host_notifier(vvq));

    file.fd = event_notifier_get_fd(virtio_queue_get_host_notifier(vvq));
    r = vhost_user_set_vring_kick(&file);
    if (r) {
        DBG("vhost_set_vring_kick failed\n");
        return r;
    }

    /* Clear and discard previous events if any. */
    event_notifier_test_and_clear(&vq->masked_notifier);

    /*
     * Init vring in unmasked state, unless guest_notifier_mask
     * will do it later.
     */
    if (!vdev->use_guest_notifier_mask) {
        /* TODO: check and handle errors. */
        vhost_virtqueue_mask(dev, vdev, idx, false);
    }

    return 0;
}

void update_mem_table(VirtIODevice *vdev)
{
    print_mem_table(vdev->vhdev);
    vhost_commit_vqs(vdev->vhdev);
    print_mem_table(vdev->vhdev);
    (void)vhost_user_set_mem_table(vdev->vhdev);
}

static int vhost_dev_set_vring_enable(struct vhost_dev *hdev, int enable)
{
    DBG("vhost_dev_set_vring_enable:\n");

    /*
     * For vhost-user devices, if VHOST_USER_F_PROTOCOL_FEATURES has not
     * been negotiated, the rings start directly in the enabled state, and
     * .vhost_set_vring_enable callback will fail since
     * VHOST_USER_SET_VRING_ENABLE is not supported.
     */
    if (!virtio_has_feature(hdev->backend_features,
                            VHOST_USER_F_PROTOCOL_FEATURES)) {
        DBG("Does not have VHOST_USER_F_PROTOCOL_FEATURES\n");
        return 0;
    }

    return vhost_user_set_vring_enable(hdev, enable);
}

/* Host notifiers must be enabled at this point. */
int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings)
{
    int i, r;

    hdev->started = true;
    hdev->vdev = vdev;

    r = vhost_dev_set_features(hdev, hdev->log_enabled);
    if (r < 0) {
        return r;
    }

    /* TODO: check if this is needed */
    if (vhost_dev_has_iommu(hdev)) {
        DBG("memory_listener_register?\n");
    }

    vhost_commit_mem_regions(hdev);

    for (i = 0; i < hdev->nvqs; ++i) {
        r = vhost_virtqueue_start(hdev,
                                  vdev,
                                  hdev->vqs + i,
                                  hdev->vq_index + i);
        if (r < 0) {
            DBG("Fail vhost_virtqueue_start\n");
            return r;
        }
    }

    if (vrings) {
        r = vhost_dev_set_vring_enable(hdev, true);
        if (r) {
            DBG("Fail vhost_dev_set_vring_enable\n");
            return r;
        }
    }

    r = vhost_user_dev_start(hdev, true);
    if (r) {
        DBG("Fail vhost_dev_set_vring_enable\n");
        return r;
    }

    return 0;
}


int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config,
                         uint32_t config_len)
{
    return vhost_user_get_config(hdev, config, config_len);
}

int vhost_dev_set_config(struct vhost_dev *hdev, const uint8_t *data,
                         uint32_t offset, uint32_t size, uint32_t flags)
{
    return vhost_user_set_config(hdev, data, offset, size, flags);
}

void vhost_dev_set_config_notifier(struct vhost_dev *hdev,
                                   const VhostDevConfigOps *ops)
{
    hdev->config_ops = ops;
}

int vhost_dev_prepare_inflight(struct vhost_dev *hdev, VirtIODevice *vdev)
{
    int r;

    /*
     * TODO: Check if we need that
     * if (hdev->vhost_ops->vhost_get_inflight_fd == NULL ||
     *     hdev->vhost_ops->vhost_set_inflight_fd == NULL) {
     *     return 0;
     * }
     */

    hdev->vdev = vdev;

    r = vhost_dev_set_features(hdev, hdev->log_enabled);
    if (r < 0) {
        DBG("vhost_dev_prepare_inflight failed\n");
        return r;
    }

    return 0;
}

int vhost_dev_set_inflight(struct vhost_dev *dev,
                           struct vhost_inflight *inflight)
{
    int r;

    if (inflight->addr) {
        r = vhost_user_set_inflight_fd(dev, inflight);
        if (r) {
            DBG("vhost_set_inflight_fd failed\n");
            return -1;
        }
    }

    return 0;
}

int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
                           struct vhost_inflight *inflight)
{
    int r;

    r = vhost_user_get_inflight_fd(dev, queue_size, inflight);
    if (r) {
        DBG("vhost_get_inflight_fd failed\n");
        return -1;
    }

    return 0;
}