History

vdpasim: control virtqueue support

This patch introduces the control virtqueue support for vDPA simulator. This is a requirement for supporting advanced features like multiqueue. A requirement for control virtqueue is to isolate its memory access from the rx/tx virtqueues. This is because when using vDPA device for VM, the control virqueue is not directly assigned to VM. Userspace (Qemu) will present a shadow control virtqueue to control for recording the device states. The isolation is done via the virtqueue groups and ASID support in vDPA through vhost-vdpa. The simulator is extended to have: 1) three virtqueues: RXVQ, TXVQ and CVQ (control virtqueue) 2) two virtqueue groups: group 0 contains RXVQ and TXVQ; group 1 contains CVQ 3) two address spaces and the simulator simply implements the address spaces by mapping it 1:1 to IOTLB. For the VM use cases, userspace(Qemu) may set AS 0 to group 0 and AS 1 to group 1. So we have: 1) The IOTLB for virtqueue group 0 contains the mappings of guest, so RX and TX can be assigned to guest directly. 2) The IOTLB for virtqueue group 1 contains the mappings of CVQ which is the buffers that allocated and managed by VMM only. So CVQ of vhost-vdpa is visible to VMM only. And Guest can not access the CVQ of vhost-vdpa. For the other use cases, since AS 0 is associated to all virtqueue groups by default. All virtqueues share the same mapping by default. To demonstrate the function, VIRITO_NET_F_CTRL_MACADDR is implemented in the simulator for the driver to set mac address.

Gautam Dawar committed 4 years ago

Commit bda324f1 parent cfe22683 files · +161 −20Browse files

Unified · Split

3 files
  1. drivers/vdpa/vdpa_sim/vdpa_sim.c +73 −18
  2. drivers/vdpa/vdpa_sim/vdpa_sim.h +2 −0
  3. drivers/vdpa/vdpa_sim/vdpa_sim_net.c +86 −2
drivers/vdpa/vdpa_sim/vdpa_sim.c+73−18
@@ -96,11 +96,17 @@ static void vdpasim_do_reset(struct vdpasim *vdpasim) · 95 unmodified lines
96{96{
97 int i;97 int i;
98 98
99 for (i = 0; i < vdpasim->dev_attr.nvqs; i++)99 spin_lock(&vdpasim->iommu_lock);
100
101 for (i = 0; i < vdpasim->dev_attr.nvqs; i++) {
100 vdpasim_vq_reset(vdpasim, &vdpasim->vqs[i]);102 vdpasim_vq_reset(vdpasim, &vdpasim->vqs[i]);
103 vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],
104 &vdpasim->iommu_lock);
105 }
106
107 for (i = 0; i < vdpasim->dev_attr.nas; i++)
108 vhost_iotlb_reset(&vdpasim->iommu[i]);
101 109
102 spin_lock(&vdpasim->iommu_lock);
103 vhost_iotlb_reset(vdpasim->iommu);
104 spin_unlock(&vdpasim->iommu_lock);110 spin_unlock(&vdpasim->iommu_lock);
105 111
106 vdpasim->features = 0;112 vdpasim->features = 0;
@@ -145,7 +151,7 @@ static dma_addr_t vdpasim_map_range(struct vdpasim *vdpasim, phys_addr_t paddr, · 38 unmodified lines
145 dma_addr = iova_dma_addr(&vdpasim->iova, iova);151 dma_addr = iova_dma_addr(&vdpasim->iova, iova);
146 152
147 spin_lock(&vdpasim->iommu_lock);153 spin_lock(&vdpasim->iommu_lock);
148 ret = vhost_iotlb_add_range(vdpasim->iommu, (u64)dma_addr,154 ret = vhost_iotlb_add_range(&vdpasim->iommu[0], (u64)dma_addr,
149 (u64)dma_addr + size - 1, (u64)paddr, perm);155 (u64)dma_addr + size - 1, (u64)paddr, perm);
150 spin_unlock(&vdpasim->iommu_lock);156 spin_unlock(&vdpasim->iommu_lock);
151 157
@@ -161,7 +167,7 @@ static void vdpasim_unmap_range(struct vdpasim *vdpasim, dma_addr_t dma_addr, · 9 unmodified lines
161 size_t size)167 size_t size)
162{168{
163 spin_lock(&vdpasim->iommu_lock);169 spin_lock(&vdpasim->iommu_lock);
164 vhost_iotlb_del_range(vdpasim->iommu, (u64)dma_addr,170 vhost_iotlb_del_range(&vdpasim->iommu[0], (u64)dma_addr,
165 (u64)dma_addr + size - 1);171 (u64)dma_addr + size - 1);
166 spin_unlock(&vdpasim->iommu_lock);172 spin_unlock(&vdpasim->iommu_lock);
167 173
@@ -250,8 +256,9 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr) · 82 unmodified lines
250 else256 else
251 ops = &vdpasim_config_ops;257 ops = &vdpasim_config_ops;
252 258
253 vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, 1,259 vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
254 1, dev_attr->name, false);260 dev_attr->ngroups, dev_attr->nas,
261 dev_attr->name, false);
255 if (IS_ERR(vdpasim)) {262 if (IS_ERR(vdpasim)) {
256 ret = PTR_ERR(vdpasim);263 ret = PTR_ERR(vdpasim);
257 goto err_alloc;264 goto err_alloc;
@@ -278,16 +285,20 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr) · 20 unmodified lines
278 if (!vdpasim->vqs)285 if (!vdpasim->vqs)
279 goto err_iommu;286 goto err_iommu;
280 287
281 vdpasim->iommu = vhost_iotlb_alloc(max_iotlb_entries, 0);288 vdpasim->iommu = kmalloc_array(vdpasim->dev_attr.nas,
289 sizeof(*vdpasim->iommu), GFP_KERNEL);
282 if (!vdpasim->iommu)290 if (!vdpasim->iommu)
283 goto err_iommu;291 goto err_iommu;
284 292
293 for (i = 0; i < vdpasim->dev_attr.nas; i++)
294 vhost_iotlb_init(&vdpasim->iommu[i], 0, 0);
295
285 vdpasim->buffer = kvmalloc(dev_attr->buffer_size, GFP_KERNEL);296 vdpasim->buffer = kvmalloc(dev_attr->buffer_size, GFP_KERNEL);
286 if (!vdpasim->buffer)297 if (!vdpasim->buffer)
287 goto err_iommu;298 goto err_iommu;
288 299
289 for (i = 0; i < dev_attr->nvqs; i++)300 for (i = 0; i < dev_attr->nvqs; i++)
290 vringh_set_iotlb(&vdpasim->vqs[i].vring, vdpasim->iommu,301 vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0],
291 &vdpasim->iommu_lock);302 &vdpasim->iommu_lock);
292 303
293 ret = iova_cache_get();304 ret = iova_cache_get();
@@ -401,7 +412,11 @@ static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa) · 107 unmodified lines
401 412
402static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx)413static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx)
403{414{
404 return 0;415 /* RX and TX belongs to group 0, CVQ belongs to group 1 */
416 if (idx == 2)
417 return 1;
418 else
419 return 0;
405}420}
406 421
407static u64 vdpasim_get_device_features(struct vdpa_device *vdpa)422static u64 vdpasim_get_device_features(struct vdpa_device *vdpa)
@@ -539,20 +554,53 @@ static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa) · 131 unmodified lines
539 return range;554 return range;
540}555}
541 556
557static int vdpasim_set_group_asid(struct vdpa_device *vdpa, unsigned int group,
558 unsigned int asid)
559{
560 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
561 struct vhost_iotlb *iommu;
562 int i;
563
564 if (group > vdpasim->dev_attr.ngroups)
565 return -EINVAL;
566
567 if (asid > vdpasim->dev_attr.nas)
568 return -EINVAL;
569
570 iommu = &vdpasim->iommu[asid];
571
572 spin_lock(&vdpasim->lock);
573
574 for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
575 if (vdpasim_get_vq_group(vdpa, i) == group)
576 vringh_set_iotlb(&vdpasim->vqs[i].vring, iommu,
577 &vdpasim->iommu_lock);
578
579 spin_unlock(&vdpasim->lock);
580
581 return 0;
582}
583
542static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,584static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
543 struct vhost_iotlb *iotlb)585 struct vhost_iotlb *iotlb)
544{586{
545 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);587 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
546 struct vhost_iotlb_map *map;588 struct vhost_iotlb_map *map;
589 struct vhost_iotlb *iommu;
547 u64 start = 0ULL, last = 0ULL - 1;590 u64 start = 0ULL, last = 0ULL - 1;
548 int ret;591 int ret;
549 592
593 if (asid >= vdpasim->dev_attr.nas)
594 return -EINVAL;
595
550 spin_lock(&vdpasim->iommu_lock);596 spin_lock(&vdpasim->iommu_lock);
551 vhost_iotlb_reset(vdpasim->iommu);597
598 iommu = &vdpasim->iommu[asid];
599 vhost_iotlb_reset(iommu);
552 600
553 for (map = vhost_iotlb_itree_first(iotlb, start, last); map;601 for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
554 map = vhost_iotlb_itree_next(map, start, last)) {602 map = vhost_iotlb_itree_next(map, start, last)) {
555 ret = vhost_iotlb_add_range(vdpasim->iommu, map->start,603 ret = vhost_iotlb_add_range(iommu, map->start,
556 map->last, map->addr, map->perm);604 map->last, map->addr, map->perm);
557 if (ret)605 if (ret)
558 goto err;606 goto err;
@@ -561,7 +609,7 @@ static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid, · 2 unmodified lines
561 return 0;609 return 0;
562 610
563err:611err:
564 vhost_iotlb_reset(vdpasim->iommu);612 vhost_iotlb_reset(iommu);
565 spin_unlock(&vdpasim->iommu_lock);613 spin_unlock(&vdpasim->iommu_lock);
566 return ret;614 return ret;
567}615}
@@ -573,9 +621,12 @@ static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid, · 5 unmodified lines
573 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);621 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
574 int ret;622 int ret;
575 623
624 if (asid >= vdpasim->dev_attr.nas)
625 return -EINVAL;
626
576 spin_lock(&vdpasim->iommu_lock);627 spin_lock(&vdpasim->iommu_lock);
577 ret = vhost_iotlb_add_range_ctx(vdpasim->iommu, iova, iova + size - 1,628 ret = vhost_iotlb_add_range_ctx(&vdpasim->iommu[asid], iova,
578 pa, perm, opaque);629 iova + size - 1, pa, perm, opaque);
579 spin_unlock(&vdpasim->iommu_lock);630 spin_unlock(&vdpasim->iommu_lock);
580 631
581 return ret;632 return ret;
@@ -586,8 +637,11 @@ static int vdpasim_dma_unmap(struct vdpa_device *vdpa, unsigned int asid, · 4 unmodified lines
586{637{
587 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);638 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
588 639
640 if (asid >= vdpasim->dev_attr.nas)
641 return -EINVAL;
642
589 spin_lock(&vdpasim->iommu_lock);643 spin_lock(&vdpasim->iommu_lock);
590 vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);644 vhost_iotlb_del_range(&vdpasim->iommu[asid], iova, iova + size - 1);
591 spin_unlock(&vdpasim->iommu_lock);645 spin_unlock(&vdpasim->iommu_lock);
592 646
593 return 0;647 return 0;
@@ -611,8 +665,7 @@ static void vdpasim_free(struct vdpa_device *vdpa) · 17 unmodified lines
611 }665 }
612 666
613 kvfree(vdpasim->buffer);667 kvfree(vdpasim->buffer);
614 if (vdpasim->iommu)668 vhost_iotlb_free(vdpasim->iommu);
615 vhost_iotlb_free(vdpasim->iommu);
616 kfree(vdpasim->vqs);669 kfree(vdpasim->vqs);
617 kfree(vdpasim->config);670 kfree(vdpasim->config);
618}671}
@@ -643,6 +696,7 @@ static const struct vdpa_config_ops vdpasim_config_ops = { · 24 unmodified lines
643 .set_config = vdpasim_set_config,696 .set_config = vdpasim_set_config,
644 .get_generation = vdpasim_get_generation,697 .get_generation = vdpasim_get_generation,
645 .get_iova_range = vdpasim_get_iova_range,698 .get_iova_range = vdpasim_get_iova_range,
699 .set_group_asid = vdpasim_set_group_asid,
646 .dma_map = vdpasim_dma_map,700 .dma_map = vdpasim_dma_map,
647 .dma_unmap = vdpasim_dma_unmap,701 .dma_unmap = vdpasim_dma_unmap,
648 .free = vdpasim_free,702 .free = vdpasim_free,
@@ -674,6 +728,7 @@ static const struct vdpa_config_ops vdpasim_batch_config_ops = { · 25 unmodified lines
674 .set_config = vdpasim_set_config,728 .set_config = vdpasim_set_config,
675 .get_generation = vdpasim_get_generation,729 .get_generation = vdpasim_get_generation,
676 .get_iova_range = vdpasim_get_iova_range,730 .get_iova_range = vdpasim_get_iova_range,
731 .set_group_asid = vdpasim_set_group_asid,
677 .set_map = vdpasim_set_map,732 .set_map = vdpasim_set_map,
678 .free = vdpasim_free,733 .free = vdpasim_free,
679};734};
drivers/vdpa/vdpa_sim/vdpa_sim.h+2−0
@@ -41,6 +41,8 @@ struct vdpasim_dev_attr { · 40 unmodified lines
41 size_t buffer_size;41 size_t buffer_size;
42 int nvqs;42 int nvqs;
43 u32 id;43 u32 id;
44 u32 ngroups;
45 u32 nas;
44 46
45 work_func_t work_fn;47 work_func_t work_fn;
46 void (*get_config)(struct vdpasim *vdpasim, void *config);48 void (*get_config)(struct vdpasim *vdpasim, void *config);
drivers/vdpa/vdpa_sim/vdpa_sim_net.c+86−2
@@ -27,9 +27,14 @@ · 26 unmodified lines
27 27
28#define VDPASIM_NET_FEATURES (VDPASIM_FEATURES | \28#define VDPASIM_NET_FEATURES (VDPASIM_FEATURES | \
29 (1ULL << VIRTIO_NET_F_MAC) | \29 (1ULL << VIRTIO_NET_F_MAC) | \
30 (1ULL << VIRTIO_NET_F_MTU))30 (1ULL << VIRTIO_NET_F_MTU) | \
31 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
32 (1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR))
31 33
32#define VDPASIM_NET_VQ_NUM 234/* 3 virtqueues, 2 address spaces, 2 virtqueue groups */
35#define VDPASIM_NET_VQ_NUM 3
36#define VDPASIM_NET_AS_NUM 2
37#define VDPASIM_NET_GROUP_NUM 2
33 38
34static void vdpasim_net_complete(struct vdpasim_virtqueue *vq, size_t len)39static void vdpasim_net_complete(struct vdpasim_virtqueue *vq, size_t len)
35{40{
@@ -63,6 +68,81 @@ static bool receive_filter(struct vdpasim *vdpasim, size_t len) · 27 unmodified lines
63 return false;68 return false;
64}69}
65 70
71static virtio_net_ctrl_ack vdpasim_handle_ctrl_mac(struct vdpasim *vdpasim,
72 u8 cmd)
73{
74 struct virtio_net_config *vio_config = vdpasim->config;
75 struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
76 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
77 size_t read;
78
79 switch (cmd) {
80 case VIRTIO_NET_CTRL_MAC_ADDR_SET:
81 read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov,
82 vio_config->mac, ETH_ALEN);
83 if (read == ETH_ALEN)
84 status = VIRTIO_NET_OK;
85 break;
86 default:
87 break;
88 }
89
90 return status;
91}
92
93static void vdpasim_handle_cvq(struct vdpasim *vdpasim)
94{
95 struct vdpasim_virtqueue *cvq = &vdpasim->vqs[2];
96 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
97 struct virtio_net_ctrl_hdr ctrl;
98 size_t read, write;
99 int err;
100
101 if (!(vdpasim->features & (1ULL << VIRTIO_NET_F_CTRL_VQ)))
102 return;
103
104 if (!cvq->ready)
105 return;
106
107 while (true) {
108 err = vringh_getdesc_iotlb(&cvq->vring, &cvq->in_iov,
109 &cvq->out_iov,
110 &cvq->head, GFP_ATOMIC);
111 if (err <= 0)
112 break;
113
114 read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->in_iov, &ctrl,
115 sizeof(ctrl));
116 if (read != sizeof(ctrl))
117 break;
118
119 switch (ctrl.class) {
120 case VIRTIO_NET_CTRL_MAC:
121 status = vdpasim_handle_ctrl_mac(vdpasim, ctrl.cmd);
122 break;
123 default:
124 break;
125 }
126
127 /* Make sure data is wrote before advancing index */
128 smp_wmb();
129
130 write = vringh_iov_push_iotlb(&cvq->vring, &cvq->out_iov,
131 &status, sizeof(status));
132 vringh_complete_iotlb(&cvq->vring, cvq->head, write);
133 vringh_kiov_cleanup(&cvq->in_iov);
134 vringh_kiov_cleanup(&cvq->out_iov);
135
136 /* Make sure used is visible before rasing the interrupt. */
137 smp_wmb();
138
139 local_bh_disable();
140 if (cvq->cb)
141 cvq->cb(cvq->private);
142 local_bh_enable();
143 }
144}
145
66static void vdpasim_net_work(struct work_struct *work)146static void vdpasim_net_work(struct work_struct *work)
67{147{
68 struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);148 struct vdpasim *vdpasim = container_of(work, struct vdpasim, work);
@@ -77,6 +157,8 @@ static void vdpasim_net_work(struct work_struct *work) · 8 unmodified lines
77 if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))157 if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
78 goto out;158 goto out;
79 159
160 vdpasim_handle_cvq(vdpasim);
161
80 if (!txq->ready || !rxq->ready)162 if (!txq->ready || !rxq->ready)
81 goto out;163 goto out;
82 164
@@ -162,6 +244,8 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name, · 79 unmodified lines
162 dev_attr.id = VIRTIO_ID_NET;244 dev_attr.id = VIRTIO_ID_NET;
163 dev_attr.supported_features = VDPASIM_NET_FEATURES;245 dev_attr.supported_features = VDPASIM_NET_FEATURES;
164 dev_attr.nvqs = VDPASIM_NET_VQ_NUM;246 dev_attr.nvqs = VDPASIM_NET_VQ_NUM;
247 dev_attr.ngroups = VDPASIM_NET_GROUP_NUM;
248 dev_attr.nas = VDPASIM_NET_AS_NUM;
165 dev_attr.config_size = sizeof(struct virtio_net_config);249 dev_attr.config_size = sizeof(struct virtio_net_config);
166 dev_attr.get_config = vdpasim_net_get_config;250 dev_attr.get_config = vdpasim_net_get_config;
167 dev_attr.work_fn = vdpasim_net_work;251 dev_attr.work_fn = vdpasim_net_work;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t