* [PATCH RFC don't apply] vdpa_sim: endian-ness for config space
@ 2020-07-15 13:58 Michael S. Tsirkin
2020-07-15 14:02 ` Jason Wang
0 siblings, 1 reply; 4+ messages in thread
From: Michael S. Tsirkin @ 2020-07-15 13:58 UTC (permalink / raw)
To: linux-kernel; +Cc: Jason Wang, virtualization
VDPA sim stores config space as native endian, but that
is wrong: modern guests expect LE.
I coded up the following to fix it up, but it is wrong too:
vdpasim_create is called before guest features are known.
So what should we do? New ioctl to specify the interface used?
More ideas?
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
drivers/vdpa/vdpa_sim/vdpa_sim.c | 22 ++++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
index a9bc5e0fb353..cc754ae0ec15 100644
--- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
+++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
@@ -24,6 +24,7 @@
#include <linux/etherdevice.h>
#include <linux/vringh.h>
#include <linux/vdpa.h>
+#include <linux/virtio_byteorder.h>
#include <linux/vhost_iotlb.h>
#include <uapi/linux/virtio_config.h>
#include <uapi/linux/virtio_net.h>
@@ -72,6 +73,23 @@ struct vdpasim {
u64 features;
};
+/* TODO: cross-endian support */
+static inline bool vdpasim_is_little_endian(struct vdpasim *vdpasim)
+{
+ return virtio_legacy_is_little_endian() ||
+ (vdpasim->features & (1ULL << VIRTIO_F_VERSION_1));
+}
+
+static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val)
+{
+ return __virtio16_to_cpu(vdpasim_is_little_endian(vdpasim), val);
+}
+
+static inline __virtio16 cpu_to_vdpasim16(struct vdpasim *vdpasim, u16 val)
+{
+ return __cpu_to_virtio16(vdpasim_is_little_endian(vdpasim), val);
+}
+
static struct vdpasim *vdpasim_dev;
static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa)
@@ -332,8 +350,8 @@ static struct vdpasim *vdpasim_create(void)
goto err_iommu;
config = &vdpasim->config;
- config->mtu = 1500;
- config->status = VIRTIO_NET_S_LINK_UP;
+ config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
+ config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
eth_random_addr(config->mac);
vringh_set_iotlb(&vdpasim->vqs[0].vring, vdpasim->iommu);
--
MST
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH RFC don't apply] vdpa_sim: endian-ness for config space
2020-07-15 13:58 [PATCH RFC don't apply] vdpa_sim: endian-ness for config space Michael S. Tsirkin
@ 2020-07-15 14:02 ` Jason Wang
2020-07-16 5:42 ` Michael S. Tsirkin
0 siblings, 1 reply; 4+ messages in thread
From: Jason Wang @ 2020-07-15 14:02 UTC (permalink / raw)
To: Michael S. Tsirkin, linux-kernel; +Cc: virtualization
On 2020/7/15 下午9:58, Michael S. Tsirkin wrote:
> VDPA sim stores config space as native endian, but that
> is wrong: modern guests expect LE.
> I coded up the following to fix it up, but it is wrong too:
> vdpasim_create is called before guest features are known.
>
> So what should we do? New ioctl to specify the interface used?
> More ideas?
>
> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Can we do the endian conversion in set_config/get_config()?
Thanks
>
>
> ---
> drivers/vdpa/vdpa_sim/vdpa_sim.c | 22 ++++++++++++++++++++--
> 1 file changed, 20 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> index a9bc5e0fb353..cc754ae0ec15 100644
> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> @@ -24,6 +24,7 @@
> #include <linux/etherdevice.h>
> #include <linux/vringh.h>
> #include <linux/vdpa.h>
> +#include <linux/virtio_byteorder.h>
> #include <linux/vhost_iotlb.h>
> #include <uapi/linux/virtio_config.h>
> #include <uapi/linux/virtio_net.h>
> @@ -72,6 +73,23 @@ struct vdpasim {
> u64 features;
> };
>
> +/* TODO: cross-endian support */
> +static inline bool vdpasim_is_little_endian(struct vdpasim *vdpasim)
> +{
> + return virtio_legacy_is_little_endian() ||
> + (vdpasim->features & (1ULL << VIRTIO_F_VERSION_1));
> +}
> +
> +static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val)
> +{
> + return __virtio16_to_cpu(vdpasim_is_little_endian(vdpasim), val);
> +}
> +
> +static inline __virtio16 cpu_to_vdpasim16(struct vdpasim *vdpasim, u16 val)
> +{
> + return __cpu_to_virtio16(vdpasim_is_little_endian(vdpasim), val);
> +}
> +
> static struct vdpasim *vdpasim_dev;
>
> static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa)
> @@ -332,8 +350,8 @@ static struct vdpasim *vdpasim_create(void)
> goto err_iommu;
>
> config = &vdpasim->config;
> - config->mtu = 1500;
> - config->status = VIRTIO_NET_S_LINK_UP;
> + config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
> + config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
> eth_random_addr(config->mac);
>
> vringh_set_iotlb(&vdpasim->vqs[0].vring, vdpasim->iommu);
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH RFC don't apply] vdpa_sim: endian-ness for config space
2020-07-15 14:02 ` Jason Wang
@ 2020-07-16 5:42 ` Michael S. Tsirkin
2020-07-16 7:42 ` Jason Wang
0 siblings, 1 reply; 4+ messages in thread
From: Michael S. Tsirkin @ 2020-07-16 5:42 UTC (permalink / raw)
To: Jason Wang; +Cc: linux-kernel, virtualization
On Wed, Jul 15, 2020 at 10:02:32PM +0800, Jason Wang wrote:
>
> On 2020/7/15 下午9:58, Michael S. Tsirkin wrote:
> > VDPA sim stores config space as native endian, but that
> > is wrong: modern guests expect LE.
> > I coded up the following to fix it up, but it is wrong too:
> > vdpasim_create is called before guest features are known.
> >
> > So what should we do? New ioctl to specify the interface used?
> > More ideas?
> >
> > Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
>
>
> Can we do the endian conversion in set_config/get_config()?
>
> Thanks
That is problematic at least from static checking point of view.
It would be reasonable to do it in vdpasim_set_features, except
legacy guests might not set features at all.
So my proposal is:
- set config in vdpasim_set_features
- document that this is where devices should initialize config
- vdpa core will maintain a "features set" flag, if get/set config
is called without set features, core will call set features
automatically with 0 value.
Thoughts?
>
> >
> >
> > ---
> > drivers/vdpa/vdpa_sim/vdpa_sim.c | 22 ++++++++++++++++++++--
> > 1 file changed, 20 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> > index a9bc5e0fb353..cc754ae0ec15 100644
> > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
> > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
> > @@ -24,6 +24,7 @@
> > #include <linux/etherdevice.h>
> > #include <linux/vringh.h>
> > #include <linux/vdpa.h>
> > +#include <linux/virtio_byteorder.h>
> > #include <linux/vhost_iotlb.h>
> > #include <uapi/linux/virtio_config.h>
> > #include <uapi/linux/virtio_net.h>
> > @@ -72,6 +73,23 @@ struct vdpasim {
> > u64 features;
> > };
> > +/* TODO: cross-endian support */
> > +static inline bool vdpasim_is_little_endian(struct vdpasim *vdpasim)
> > +{
> > + return virtio_legacy_is_little_endian() ||
> > + (vdpasim->features & (1ULL << VIRTIO_F_VERSION_1));
> > +}
> > +
> > +static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val)
> > +{
> > + return __virtio16_to_cpu(vdpasim_is_little_endian(vdpasim), val);
> > +}
> > +
> > +static inline __virtio16 cpu_to_vdpasim16(struct vdpasim *vdpasim, u16 val)
> > +{
> > + return __cpu_to_virtio16(vdpasim_is_little_endian(vdpasim), val);
> > +}
> > +
> > static struct vdpasim *vdpasim_dev;
> > static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa)
> > @@ -332,8 +350,8 @@ static struct vdpasim *vdpasim_create(void)
> > goto err_iommu;
> > config = &vdpasim->config;
> > - config->mtu = 1500;
> > - config->status = VIRTIO_NET_S_LINK_UP;
> > + config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
> > + config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
> > eth_random_addr(config->mac);
> > vringh_set_iotlb(&vdpasim->vqs[0].vring, vdpasim->iommu);
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH RFC don't apply] vdpa_sim: endian-ness for config space
2020-07-16 5:42 ` Michael S. Tsirkin
@ 2020-07-16 7:42 ` Jason Wang
0 siblings, 0 replies; 4+ messages in thread
From: Jason Wang @ 2020-07-16 7:42 UTC (permalink / raw)
To: Michael S. Tsirkin; +Cc: linux-kernel, virtualization
On 2020/7/16 下午1:42, Michael S. Tsirkin wrote:
> On Wed, Jul 15, 2020 at 10:02:32PM +0800, Jason Wang wrote:
>> On 2020/7/15 下午9:58, Michael S. Tsirkin wrote:
>>> VDPA sim stores config space as native endian, but that
>>> is wrong: modern guests expect LE.
>>> I coded up the following to fix it up, but it is wrong too:
>>> vdpasim_create is called before guest features are known.
>>>
>>> So what should we do? New ioctl to specify the interface used?
>>> More ideas?
>>>
>>> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
>>
>> Can we do the endian conversion in set_config/get_config()?
>>
>> Thanks
> That is problematic at least from static checking point of view.
I don't get here. I thought it's just leXX_to_cpu() conversion in
set_config()/get_config() as what is done in MMIO's vm_set/vm_get.
> It would be reasonable to do it in vdpasim_set_features, except
> legacy guests might not set features at all.
> So my proposal is:
> - set config in vdpasim_set_features
Just to make sure I understand here. What's the meaning of "set config"
do you mean setup the config space? If yes, and if there's a device
config space, we still need to do the endian conversion?
> - document that this is where devices should initialize config
> - vdpa core will maintain a "features set" flag, if get/set config
> is called without set features, core will call set features
> automatically with 0 value.
Ok, I think you meant checking FEATURE_OK via get_status in vDPA core?
Thanks
>
> Thoughts?
>
>
>>>
>>> ---
>>> drivers/vdpa/vdpa_sim/vdpa_sim.c | 22 ++++++++++++++++++++--
>>> 1 file changed, 20 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c
>>> index a9bc5e0fb353..cc754ae0ec15 100644
>>> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c
>>> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c
>>> @@ -24,6 +24,7 @@
>>> #include <linux/etherdevice.h>
>>> #include <linux/vringh.h>
>>> #include <linux/vdpa.h>
>>> +#include <linux/virtio_byteorder.h>
>>> #include <linux/vhost_iotlb.h>
>>> #include <uapi/linux/virtio_config.h>
>>> #include <uapi/linux/virtio_net.h>
>>> @@ -72,6 +73,23 @@ struct vdpasim {
>>> u64 features;
>>> };
>>> +/* TODO: cross-endian support */
>>> +static inline bool vdpasim_is_little_endian(struct vdpasim *vdpasim)
>>> +{
>>> + return virtio_legacy_is_little_endian() ||
>>> + (vdpasim->features & (1ULL << VIRTIO_F_VERSION_1));
>>> +}
>>> +
>>> +static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val)
>>> +{
>>> + return __virtio16_to_cpu(vdpasim_is_little_endian(vdpasim), val);
>>> +}
>>> +
>>> +static inline __virtio16 cpu_to_vdpasim16(struct vdpasim *vdpasim, u16 val)
>>> +{
>>> + return __cpu_to_virtio16(vdpasim_is_little_endian(vdpasim), val);
>>> +}
>>> +
>>> static struct vdpasim *vdpasim_dev;
>>> static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa)
>>> @@ -332,8 +350,8 @@ static struct vdpasim *vdpasim_create(void)
>>> goto err_iommu;
>>> config = &vdpasim->config;
>>> - config->mtu = 1500;
>>> - config->status = VIRTIO_NET_S_LINK_UP;
>>> + config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
>>> + config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
>>> eth_random_addr(config->mac);
>>> vringh_set_iotlb(&vdpasim->vqs[0].vring, vdpasim->iommu);
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2020-07-16 7:42 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-15 13:58 [PATCH RFC don't apply] vdpa_sim: endian-ness for config space Michael S. Tsirkin
2020-07-15 14:02 ` Jason Wang
2020-07-16 5:42 ` Michael S. Tsirkin
2020-07-16 7:42 ` Jason Wang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).