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
9696 {
9797 int i;
9898
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++) {
100102 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]);
101109
102− spin_lock(&vdpasim->iommu_lock);
103− vhost_iotlb_reset(vdpasim->iommu);
104110 spin_unlock(&vdpasim->iommu_lock);
105111
106112 vdpasim->features = 0;
@@ -145,7 +151,7 @@ static dma_addr_t vdpasim_map_range(struct vdpasim *vdpasim, phys_addr_t paddr, · 38 unmodified lines
145151 dma_addr = iova_dma_addr(&vdpasim->iova, iova);
146152
147153 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,
149155 (u64)dma_addr + size - 1, (u64)paddr, perm);
150156 spin_unlock(&vdpasim->iommu_lock);
151157
@@ -161,7 +167,7 @@ static void vdpasim_unmap_range(struct vdpasim *vdpasim, dma_addr_t dma_addr, · 9 unmodified lines
161167 size_t size)
162168 {
163169 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,
165171 (u64)dma_addr + size - 1);
166172 spin_unlock(&vdpasim->iommu_lock);
167173
@@ -250,8 +256,9 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr) · 82 unmodified lines
250256 else
251257 ops = &vdpasim_config_ops;
252258
253− vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops, 1,
254− 1, dev_attr->name, false);
259+ vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
260+ dev_attr->ngroups, dev_attr->nas,
261+ dev_attr->name, false);
255262 if (IS_ERR(vdpasim)) {
256263 ret = PTR_ERR(vdpasim);
257264 goto err_alloc;
@@ -278,16 +285,20 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr) · 20 unmodified lines
278285 if (!vdpasim->vqs)
279286 goto err_iommu;
280287
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);
282290 if (!vdpasim->iommu)
283291 goto err_iommu;
284292
293+ for (i = 0; i < vdpasim->dev_attr.nas; i++)
294+ vhost_iotlb_init(&vdpasim->iommu[i], 0, 0);
295+
285296 vdpasim->buffer = kvmalloc(dev_attr->buffer_size, GFP_KERNEL);
286297 if (!vdpasim->buffer)
287298 goto err_iommu;
288299
289300 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],
291302 &vdpasim->iommu_lock);
292303
293304 ret = iova_cache_get();
@@ -401,7 +412,11 @@ static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa) · 107 unmodified lines
401412
402413 static u32 vdpasim_get_vq_group(struct vdpa_device *vdpa, u16 idx)
403414 {
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;
405420 }
406421
407422 static 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
539554 return range;
540555 }
541556
557+static 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+
542584 static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid,
543585 struct vhost_iotlb *iotlb)
544586 {
545587 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
546588 struct vhost_iotlb_map *map;
589+ struct vhost_iotlb *iommu;
547590 u64 start = 0ULL, last = 0ULL - 1;
548591 int ret;
549592
593+ if (asid >= vdpasim->dev_attr.nas)
594+ return -EINVAL;
595+
550596 spin_lock(&vdpasim->iommu_lock);
551− vhost_iotlb_reset(vdpasim->iommu);
597+
598+ iommu = &vdpasim->iommu[asid];
599+ vhost_iotlb_reset(iommu);
552600
553601 for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
554602 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,
556604 map->last, map->addr, map->perm);
557605 if (ret)
558606 goto err;
@@ -561,7 +609,7 @@ static int vdpasim_set_map(struct vdpa_device *vdpa, unsigned int asid, · 2 unmodified lines
561609 return 0;
562610
563611 err:
564− vhost_iotlb_reset(vdpasim->iommu);
612+ vhost_iotlb_reset(iommu);
565613 spin_unlock(&vdpasim->iommu_lock);
566614 return ret;
567615 }
@@ -573,9 +621,12 @@ static int vdpasim_dma_map(struct vdpa_device *vdpa, unsigned int asid, · 5 unmodified lines
573621 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
574622 int ret;
575623
624+ if (asid >= vdpasim->dev_attr.nas)
625+ return -EINVAL;
626+
576627 spin_lock(&vdpasim->iommu_lock);
577− ret = vhost_iotlb_add_range_ctx(vdpasim->iommu, iova, iova + size - 1,
578− pa, perm, opaque);
628+ ret = vhost_iotlb_add_range_ctx(&vdpasim->iommu[asid], iova,
629+ iova + size - 1, pa, perm, opaque);
579630 spin_unlock(&vdpasim->iommu_lock);
580631
581632 return ret;
@@ -586,8 +637,11 @@ static int vdpasim_dma_unmap(struct vdpa_device *vdpa, unsigned int asid, · 4 unmodified lines
586637 {
587638 struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
588639
640+ if (asid >= vdpasim->dev_attr.nas)
641+ return -EINVAL;
642+
589643 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);
591645 spin_unlock(&vdpasim->iommu_lock);
592646
593647 return 0;
@@ -611,8 +665,7 @@ static void vdpasim_free(struct vdpa_device *vdpa) · 17 unmodified lines
611665 }
612666
613667 kvfree(vdpasim->buffer);
614− if (vdpasim->iommu)
615− vhost_iotlb_free(vdpasim->iommu);
668+ vhost_iotlb_free(vdpasim->iommu);
616669 kfree(vdpasim->vqs);
617670 kfree(vdpasim->config);
618671 }
@@ -643,6 +696,7 @@ static const struct vdpa_config_ops vdpasim_config_ops = { · 24 unmodified lines
643696 .set_config = vdpasim_set_config,
644697 .get_generation = vdpasim_get_generation,
645698 .get_iova_range = vdpasim_get_iova_range,
699+ .set_group_asid = vdpasim_set_group_asid,
646700 .dma_map = vdpasim_dma_map,
647701 .dma_unmap = vdpasim_dma_unmap,
648702 .free = vdpasim_free,
@@ -674,6 +728,7 @@ static const struct vdpa_config_ops vdpasim_batch_config_ops = { · 25 unmodified lines
674728 .set_config = vdpasim_set_config,
675729 .get_generation = vdpasim_get_generation,
676730 .get_iova_range = vdpasim_get_iova_range,
731+ .set_group_asid = vdpasim_set_group_asid,
677732 .set_map = vdpasim_set_map,
678733 .free = vdpasim_free,
679734 };
drivers/vdpa/vdpa_sim/vdpa_sim.h+2−0
@@ -41,6 +41,8 @@ struct vdpasim_dev_attr { · 40 unmodified lines
4141 size_t buffer_size;
4242 int nvqs;
4343 u32 id;
44+ u32 ngroups;
45+ u32 nas;
4446
4547 work_func_t work_fn;
4648 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
2727
2828 #define VDPASIM_NET_FEATURES (VDPASIM_FEATURES | \
2929 (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))
3133
32−#define VDPASIM_NET_VQ_NUM 2
34+/* 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
3338
3439 static void vdpasim_net_complete(struct vdpasim_virtqueue *vq, size_t len)
3540 {
@@ -63,6 +68,81 @@ static bool receive_filter(struct vdpasim *vdpasim, size_t len) · 27 unmodified lines
6368 return false;
6469 }
6570
71+static 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+
93+static 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+
66146 static void vdpasim_net_work(struct work_struct *work)
67147 {
68148 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
77157 if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
78158 goto out;
79159
160+ vdpasim_handle_cvq(vdpasim);
161+
80162 if (!txq->ready || !rxq->ready)
81163 goto out;
82164
@@ -162,6 +244,8 @@ static int vdpasim_net_dev_add(struct vdpa_mgmt_dev *mdev, const char *name, · 79 unmodified lines
162244 dev_attr.id = VIRTIO_ID_NET;
163245 dev_attr.supported_features = VDPASIM_NET_FEATURES;
164246 dev_attr.nvqs = VDPASIM_NET_VQ_NUM;
247+ dev_attr.ngroups = VDPASIM_NET_GROUP_NUM;
248+ dev_attr.nas = VDPASIM_NET_AS_NUM;
165249 dev_attr.config_size = sizeof(struct virtio_net_config);
166250 dev_attr.get_config = vdpasim_net_get_config;
167251 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