DPDK logo

Elixir Cross Referencer

  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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2016 Intel Corporation
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include <rte_memory.h>

#include "vhost.h"
#include "virtio_user_dev.h"
#include "vhost_kernel_tap.h"

struct vhost_kernel_data {
	int *vhostfds;
	int *tapfds;
};

struct vhost_memory_kernel {
	uint32_t nregions;
	uint32_t padding;
	struct vhost_memory_region regions[0];
};

/* vhost kernel ioctls */
#define VHOST_VIRTIO 0xAF
#define VHOST_GET_FEATURES _IOR(VHOST_VIRTIO, 0x00, __u64)
#define VHOST_SET_FEATURES _IOW(VHOST_VIRTIO, 0x00, __u64)
#define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
#define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
#define VHOST_SET_MEM_TABLE _IOW(VHOST_VIRTIO, 0x03, struct vhost_memory_kernel)
#define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
#define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
#define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
#define VHOST_SET_VRING_ADDR _IOW(VHOST_VIRTIO, 0x11, struct vhost_vring_addr)
#define VHOST_SET_VRING_BASE _IOW(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
#define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
#define VHOST_SET_VRING_KICK _IOW(VHOST_VIRTIO, 0x20, struct vhost_vring_file)
#define VHOST_SET_VRING_CALL _IOW(VHOST_VIRTIO, 0x21, struct vhost_vring_file)
#define VHOST_SET_VRING_ERR _IOW(VHOST_VIRTIO, 0x22, struct vhost_vring_file)
#define VHOST_NET_SET_BACKEND _IOW(VHOST_VIRTIO, 0x30, struct vhost_vring_file)

/* with below features, vhost kernel does not need to do the checksum and TSO,
 * these info will be passed to virtio_user through virtio net header.
 */
#define VHOST_KERNEL_GUEST_OFFLOADS_MASK	\
	((1ULL << VIRTIO_NET_F_GUEST_CSUM) |	\
	 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |	\
	 (1ULL << VIRTIO_NET_F_GUEST_TSO6) |	\
	 (1ULL << VIRTIO_NET_F_GUEST_ECN)  |	\
	 (1ULL << VIRTIO_NET_F_GUEST_UFO))

/* with below features, when flows from virtio_user to vhost kernel
 * (1) if flows goes up through the kernel networking stack, it does not need
 * to verify checksum, which can save CPU cycles;
 * (2) if flows goes through a Linux bridge and outside from an interface
 * (kernel driver), checksum and TSO will be done by GSO in kernel or even
 * offloaded into real physical device.
 */
#define VHOST_KERNEL_HOST_OFFLOADS_MASK		\
	((1ULL << VIRTIO_NET_F_HOST_TSO4) |	\
	 (1ULL << VIRTIO_NET_F_HOST_TSO6) |	\
	 (1ULL << VIRTIO_NET_F_CSUM))

static uint64_t max_regions = 64;

static void
get_vhost_kernel_max_regions(void)
{
	int fd;
	char buf[20] = {'\0'};

	fd = open("/sys/module/vhost/parameters/max_mem_regions", O_RDONLY);
	if (fd < 0)
		return;

	if (read(fd, buf, sizeof(buf) - 1) > 0)
		max_regions = strtoull(buf, NULL, 10);

	close(fd);
}

static int
vhost_kernel_ioctl(int fd, uint64_t request, void *arg)
{
	int ret;

	ret = ioctl(fd, request, arg);
	if (ret) {
		PMD_DRV_LOG(ERR, "Vhost-kernel ioctl %"PRIu64" failed (%s)",
				request, strerror(errno));
		return -1;
	}

	return 0;
}

static int
vhost_kernel_set_owner(struct virtio_user_dev *dev)
{
	struct vhost_kernel_data *data = dev->backend_data;

	return vhost_kernel_ioctl(data->vhostfds[0], VHOST_SET_OWNER, NULL);
}

static int
vhost_kernel_get_features(struct virtio_user_dev *dev, uint64_t *features)
{
	int ret;
	unsigned int tap_features;
	struct vhost_kernel_data *data = dev->backend_data;

	ret = vhost_kernel_ioctl(data->vhostfds[0], VHOST_GET_FEATURES, features);
	if (ret < 0) {
		PMD_DRV_LOG(ERR, "Failed to get features");
		return -1;
	}

	ret = tap_support_features(&tap_features);
	if (ret < 0) {
		PMD_DRV_LOG(ERR, "Failed to get TAP features");
		return -1;
	}

	/* with tap as the backend, all these features are supported
	 * but not claimed by vhost-net, so we add them back when
	 * reporting to upper layer.
	 */
	if (tap_features & IFF_VNET_HDR) {
		*features |= VHOST_KERNEL_GUEST_OFFLOADS_MASK;
		*features |= VHOST_KERNEL_HOST_OFFLOADS_MASK;
	}

	/* vhost_kernel will not declare this feature, but it does
	 * support multi-queue.
	 */
	if (tap_features & IFF_MULTI_QUEUE)
		*features |= (1ull << VIRTIO_NET_F_MQ);

	return 0;
}

static int
vhost_kernel_set_features(struct virtio_user_dev *dev, uint64_t features)
{
	struct vhost_kernel_data *data = dev->backend_data;

	/* We don't need memory protection here */
	features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
	/* VHOST kernel does not know about below flags */
	features &= ~VHOST_KERNEL_GUEST_OFFLOADS_MASK;
	features &= ~VHOST_KERNEL_HOST_OFFLOADS_MASK;
	features &= ~(1ULL << VIRTIO_NET_F_MQ);

	return vhost_kernel_ioctl(data->vhostfds[0], VHOST_SET_FEATURES, &features);
}

static int
add_memseg_list(const struct rte_memseg_list *msl, void *arg)
{
	struct vhost_memory_kernel *vm = arg;
	struct vhost_memory_region *mr;
	void *start_addr;
	uint64_t len;

	if (msl->external)
		return 0;

	if (vm->nregions >= max_regions)
		return -1;

	start_addr = msl->base_va;
	len = msl->page_sz * msl->memseg_arr.len;

	mr = &vm->regions[vm->nregions++];

	mr->guest_phys_addr = (uint64_t)(uintptr_t)start_addr;
	mr->userspace_addr = (uint64_t)(uintptr_t)start_addr;
	mr->memory_size = len;
	mr->mmap_offset = 0; /* flags_padding */

	PMD_DRV_LOG(DEBUG, "index=%u addr=%p len=%" PRIu64,
			vm->nregions - 1, start_addr, len);

	return 0;
}

/* By default, vhost kernel module allows 64 regions, but DPDK may
 * have much more memory regions. Below function will treat each
 * contiguous memory space reserved by DPDK as one region.
 */
static int
vhost_kernel_set_memory_table(struct virtio_user_dev *dev)
{
	struct vhost_kernel_data *data = dev->backend_data;
	struct vhost_memory_kernel *vm;
	int ret;

	vm = malloc(sizeof(struct vhost_memory_kernel) +
			max_regions *
			sizeof(struct vhost_memory_region));
	if (!vm)
		goto err;

	vm->nregions = 0;
	vm->padding = 0;

	/*
	 * The memory lock has already been taken by memory subsystem
	 * or virtio_user_start_device().
	 */
	ret = rte_memseg_list_walk_thread_unsafe(add_memseg_list, vm);
	if (ret < 0)
		goto err_free;

	ret = vhost_kernel_ioctl(data->vhostfds[0], VHOST_SET_MEM_TABLE, vm);
	if (ret < 0)
		goto err_free;

	free(vm);

	return 0;
err_free:
	free(vm);
err:
	PMD_DRV_LOG(ERR, "Failed to set memory table");
	return -1;
}

static int
vhost_kernel_set_vring(struct virtio_user_dev *dev, uint64_t req, struct vhost_vring_state *state)
{
	int ret, fd;
	unsigned int index = state->index;
	struct vhost_kernel_data *data = dev->backend_data;

	/* Convert from queue index to queue-pair & offset */
	fd = data->vhostfds[state->index / 2];
	state->index %= 2;

	ret = vhost_kernel_ioctl(fd, req, state);
	if (ret < 0) {
		PMD_DRV_LOG(ERR, "Failed to set vring (request %" PRIu64 ")", req);
		return -1;
	}

	/* restore index back to queue index */
	state->index = index;

	return 0;
}

static int
vhost_kernel_set_vring_num(struct virtio_user_dev *dev, struct vhost_vring_state *state)
{
	return vhost_kernel_set_vring(dev, VHOST_SET_VRING_NUM, state);
}

static int
vhost_kernel_set_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
{
	return vhost_kernel_set_vring(dev, VHOST_SET_VRING_BASE, state);
}

static int
vhost_kernel_get_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
{
	return vhost_kernel_set_vring(dev, VHOST_GET_VRING_BASE, state);
}

static int
vhost_kernel_set_vring_file(struct virtio_user_dev *dev, uint64_t req,
		struct vhost_vring_file *file)
{
	int ret, fd;
	unsigned int index = file->index;
	struct vhost_kernel_data *data = dev->backend_data;

	/* Convert from queue index to queue-pair & offset */
	fd = data->vhostfds[file->index / 2];
	file->index %= 2;

	ret = vhost_kernel_ioctl(fd, req, file);
	if (ret < 0) {
		PMD_DRV_LOG(ERR, "Failed to set vring file (request %" PRIu64 ")", req);
		return -1;
	}

	/* restore index back to queue index */
	file->index = index;

	return 0;
}

static int
vhost_kernel_set_vring_kick(struct virtio_user_dev *dev, struct vhost_vring_file *file)
{
	return vhost_kernel_set_vring_file(dev, VHOST_SET_VRING_KICK, file);
}

static int
vhost_kernel_set_vring_call(struct virtio_user_dev *dev, struct vhost_vring_file *file)
{
	return vhost_kernel_set_vring_file(dev, VHOST_SET_VRING_CALL, file);
}

static int
vhost_kernel_set_vring_addr(struct virtio_user_dev *dev, struct vhost_vring_addr *addr)
{
	int ret, fd;
	unsigned int index = addr->index;
	struct vhost_kernel_data *data = dev->backend_data;

	/* Convert from queue index to queue-pair & offset */
	fd = data->vhostfds[addr->index / 2];
	addr->index %= 2;

	ret = vhost_kernel_ioctl(fd, VHOST_SET_VRING_ADDR, addr);
	if (ret < 0) {
		PMD_DRV_LOG(ERR, "Failed to set vring address");
		return -1;
	}

	/* restore index back to queue index */
	addr->index = index;

	return 0;
}

static int
vhost_kernel_get_status(struct virtio_user_dev *dev __rte_unused, uint8_t *status __rte_unused)
{
	return -ENOTSUP;
}

static int
vhost_kernel_set_status(struct virtio_user_dev *dev __rte_unused, uint8_t status __rte_unused)
{
	return -ENOTSUP;
}

/**
 * Set up environment to talk with a vhost kernel backend.
 *
 * @return
 *   - (-1) if fail to set up;
 *   - (>=0) if successful.
 */
static int
vhost_kernel_setup(struct virtio_user_dev *dev)
{
	int vhostfd;
	uint32_t q, i;
	struct vhost_kernel_data *data;

	data = malloc(sizeof(*data));
	if (!data) {
		PMD_INIT_LOG(ERR, "(%s) Failed to allocate Vhost-kernel data", dev->path);
		return -1;
	}

	data->vhostfds = malloc(dev->max_queue_pairs * sizeof(int));
	if (!data->vhostfds) {
		PMD_INIT_LOG(ERR, "(%s) Failed to allocate Vhost FDs", dev->path);
		goto err_data;
	}
	data->tapfds = malloc(dev->max_queue_pairs * sizeof(int));
	if (!data->tapfds) {
		PMD_INIT_LOG(ERR, "(%s) Failed to allocate TAP FDs", dev->path);
		goto err_vhostfds;
	}

	for (q = 0; q < dev->max_queue_pairs; ++q) {
		data->vhostfds[q] = -1;
		data->tapfds[q] = -1;
	}

	get_vhost_kernel_max_regions();

	for (i = 0; i < dev->max_queue_pairs; ++i) {
		vhostfd = open(dev->path, O_RDWR);
		if (vhostfd < 0) {
			PMD_DRV_LOG(ERR, "fail to open %s, %s", dev->path, strerror(errno));
			goto err_tapfds;
		}

		data->vhostfds[i] = vhostfd;
	}

	dev->backend_data = data;

	return 0;

err_tapfds:
	for (i = 0; i < dev->max_queue_pairs; i++)
		if (data->vhostfds[i] >= 0)
			close(data->vhostfds[i]);

	free(data->tapfds);
err_vhostfds:
	free(data->vhostfds);
err_data:
	free(data);

	return -1;
}

static int
vhost_kernel_destroy(struct virtio_user_dev *dev)
{
	struct vhost_kernel_data *data = dev->backend_data;
	uint32_t i;

	if (!data)
		return 0;

	for (i = 0; i < dev->max_queue_pairs; ++i) {
		if (data->vhostfds[i] >= 0)
			close(data->vhostfds[i]);
		if (data->tapfds[i] >= 0)
			close(data->tapfds[i]);
	}

	free(data->vhostfds);
	free(data->tapfds);
	free(data);
	dev->backend_data = NULL;

	return 0;
}

static int
vhost_kernel_set_backend(int vhostfd, int tapfd)
{
	struct vhost_vring_file f;

	f.fd = tapfd;
	f.index = 0;
	if (ioctl(vhostfd, VHOST_NET_SET_BACKEND, &f) < 0) {
		PMD_DRV_LOG(ERR, "VHOST_NET_SET_BACKEND fails, %s",
				strerror(errno));
		return -1;
	}

	f.index = 1;
	if (ioctl(vhostfd, VHOST_NET_SET_BACKEND, &f) < 0) {
		PMD_DRV_LOG(ERR, "VHOST_NET_SET_BACKEND fails, %s",
				strerror(errno));
		return -1;
	}

	return 0;
}

static int
vhost_kernel_enable_queue_pair(struct virtio_user_dev *dev,
			       uint16_t pair_idx,
			       int enable)
{
	int hdr_size;
	int vhostfd;
	int tapfd;
	int req_mq = (dev->max_queue_pairs > 1);
	struct vhost_kernel_data *data = dev->backend_data;

	vhostfd = data->vhostfds[pair_idx];

	if (dev->qp_enabled[pair_idx] == enable)
		return 0;

	if (!enable) {
		tapfd = data->tapfds[pair_idx];
		if (vhost_kernel_set_backend(vhostfd, -1) < 0) {
			PMD_DRV_LOG(ERR, "fail to set backend for vhost kernel");
			return -1;
		}
		if (req_mq && vhost_kernel_tap_set_queue(tapfd, false) < 0) {
			PMD_DRV_LOG(ERR, "fail to disable tap for vhost kernel");
			return -1;
		}
		dev->qp_enabled[pair_idx] = false;
		return 0;
	}

	if (data->tapfds[pair_idx] >= 0) {
		tapfd = data->tapfds[pair_idx];
		if (vhost_kernel_tap_set_offload(tapfd, dev->features) == -1)
			return -1;
		if (req_mq && vhost_kernel_tap_set_queue(tapfd, true) < 0) {
			PMD_DRV_LOG(ERR, "fail to enable tap for vhost kernel");
			return -1;
		}
		goto set_backend;
	}

	if ((dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) ||
	    (dev->features & (1ULL << VIRTIO_F_VERSION_1)))
		hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
	else
		hdr_size = sizeof(struct virtio_net_hdr);

	tapfd = vhost_kernel_open_tap(&dev->ifname, hdr_size, req_mq,
			 (char *)dev->mac_addr, dev->features);
	if (tapfd < 0) {
		PMD_DRV_LOG(ERR, "fail to open tap for vhost kernel");
		return -1;
	}

	data->tapfds[pair_idx] = tapfd;

set_backend:
	if (vhost_kernel_set_backend(vhostfd, tapfd) < 0) {
		PMD_DRV_LOG(ERR, "fail to set backend for vhost kernel");
		return -1;
	}

	dev->qp_enabled[pair_idx] = true;
	return 0;
}

static int
vhost_kernel_get_backend_features(uint64_t *features)
{
	*features = 0;

	return 0;
}

static int
vhost_kernel_update_link_state(struct virtio_user_dev *dev __rte_unused)
{
	/* Nothing to update (Maybe get TAP interface link state?) */
	return 0;
}

static int
vhost_kernel_get_intr_fd(struct virtio_user_dev *dev __rte_unused)
{
	/* No link state interrupt with Vhost-kernel */
	return -1;
}

struct virtio_user_backend_ops virtio_ops_kernel = {
	.setup = vhost_kernel_setup,
	.destroy = vhost_kernel_destroy,
	.get_backend_features = vhost_kernel_get_backend_features,
	.set_owner = vhost_kernel_set_owner,
	.get_features = vhost_kernel_get_features,
	.set_features = vhost_kernel_set_features,
	.set_memory_table = vhost_kernel_set_memory_table,
	.set_vring_num = vhost_kernel_set_vring_num,
	.set_vring_base = vhost_kernel_set_vring_base,
	.get_vring_base = vhost_kernel_get_vring_base,
	.set_vring_call = vhost_kernel_set_vring_call,
	.set_vring_kick = vhost_kernel_set_vring_kick,
	.set_vring_addr = vhost_kernel_set_vring_addr,
	.get_status = vhost_kernel_get_status,
	.set_status = vhost_kernel_set_status,
	.enable_qp = vhost_kernel_enable_queue_pair,
	.update_link_state = vhost_kernel_update_link_state,
	.get_intr_fd = vhost_kernel_get_intr_fd,
};