diff mbox

[v4,07/13] virtio-crypto: set capacity of algorithms supported

Message ID 1475051152-400276-8-git-send-email-arei.gonglei@huawei.com (mailing list archive)
State New, archived
Headers show

Commit Message

Gonglei (Arei) Sept. 28, 2016, 8:25 a.m. UTC
Expose the capacity of algorithms supported by
virtio crypto device to the frontend driver using
pci configuration space.

Signed-off-by: Gonglei <arei.gonglei@huawei.com>
---
 hw/virtio/virtio-crypto.c         | 32 +++++++++++++++++++++++++++++++-
 include/hw/virtio/virtio-crypto.h | 14 ++++++++++++++
 2 files changed, 45 insertions(+), 1 deletion(-)

Comments

Stefan Hajnoczi Oct. 4, 2016, 9:46 a.m. UTC | #1
On Wed, Sep 28, 2016 at 04:25:46PM +0800, Gonglei wrote:
>  static void virtio_crypto_get_config(VirtIODevice *vdev, uint8_t *config)
>  {
> -
> +    VirtIOCrypto *c = VIRTIO_CRYPTO(vdev);
> +    struct virtio_crypto_config crypto_cfg;
> +
> +    crypto_cfg.status = c->status;
> +    crypto_cfg.max_dataqueues = c->max_queues;
> +    crypto_cfg.crypto_services = c->conf.crypto_services;
> +    crypto_cfg.cipher_algo_l = c->conf.cipher_algo_l;
> +    crypto_cfg.cipher_algo_h = c->conf.cipher_algo_h;
> +    crypto_cfg.hash_algo = c->conf.hash_algo;
> +    crypto_cfg.mac_algo_l = c->conf.mac_algo_l;
> +    crypto_cfg.mac_algo_h = c->conf.mac_algo_h;
> +    crypto_cfg.aead_algo = c->conf.aead_algo;
> +
> +    memcpy(config, &crypto_cfg, c->config_size);
>  }

What about endianness?  For example, if the host is big-endian then this
VIRTIO 1.0 device needs to byteswap multi-byte fields.  There is a
family of functions to help with this: virtio_stl_p().
Gonglei (Arei) Oct. 5, 2016, 3:30 a.m. UTC | #2
> -----Original Message-----
> From: virtio-dev@lists.oasis-open.org [mailto:virtio-dev@lists.oasis-open.org]
> On Behalf Of Stefan Hajnoczi
> Sent: Tuesday, October 04, 2016 5:46 PM
> Subject: [virtio-dev] Re: [PATCH v4 07/13] virtio-crypto: set capacity of
> algorithms supported
> 
> On Wed, Sep 28, 2016 at 04:25:46PM +0800, Gonglei wrote:
> >  static void virtio_crypto_get_config(VirtIODevice *vdev, uint8_t *config)
> >  {
> > -
> > +    VirtIOCrypto *c = VIRTIO_CRYPTO(vdev);
> > +    struct virtio_crypto_config crypto_cfg;
> > +
> > +    crypto_cfg.status = c->status;
> > +    crypto_cfg.max_dataqueues = c->max_queues;
> > +    crypto_cfg.crypto_services = c->conf.crypto_services;
> > +    crypto_cfg.cipher_algo_l = c->conf.cipher_algo_l;
> > +    crypto_cfg.cipher_algo_h = c->conf.cipher_algo_h;
> > +    crypto_cfg.hash_algo = c->conf.hash_algo;
> > +    crypto_cfg.mac_algo_l = c->conf.mac_algo_l;
> > +    crypto_cfg.mac_algo_h = c->conf.mac_algo_h;
> > +    crypto_cfg.aead_algo = c->conf.aead_algo;
> > +
> > +    memcpy(config, &crypto_cfg, c->config_size);
> >  }
> 
> What about endianness?  For example, if the host is big-endian then this
> VIRTIO 1.0 device needs to byteswap multi-byte fields.  There is a
> family of functions to help with this: virtio_stl_p().

I did that in v1. Michael told me that Virtio-1.0 devices are always little-endian, so
I removed those helper functions in the following functions. But after this version,
the virtio-crypto device isn't virtio-1.0 device by default, so I should use them again.


Regards,
-Gonglei
Stefan Hajnoczi Oct. 5, 2016, 12:55 p.m. UTC | #3
On Wed, Oct 05, 2016 at 03:30:42AM +0000, Gonglei (Arei) wrote:
> 
> > -----Original Message-----
> > From: virtio-dev@lists.oasis-open.org [mailto:virtio-dev@lists.oasis-open.org]
> > On Behalf Of Stefan Hajnoczi
> > Sent: Tuesday, October 04, 2016 5:46 PM
> > Subject: [virtio-dev] Re: [PATCH v4 07/13] virtio-crypto: set capacity of
> > algorithms supported
> > 
> > On Wed, Sep 28, 2016 at 04:25:46PM +0800, Gonglei wrote:
> > >  static void virtio_crypto_get_config(VirtIODevice *vdev, uint8_t *config)
> > >  {
> > > -
> > > +    VirtIOCrypto *c = VIRTIO_CRYPTO(vdev);
> > > +    struct virtio_crypto_config crypto_cfg;
> > > +
> > > +    crypto_cfg.status = c->status;
> > > +    crypto_cfg.max_dataqueues = c->max_queues;
> > > +    crypto_cfg.crypto_services = c->conf.crypto_services;
> > > +    crypto_cfg.cipher_algo_l = c->conf.cipher_algo_l;
> > > +    crypto_cfg.cipher_algo_h = c->conf.cipher_algo_h;
> > > +    crypto_cfg.hash_algo = c->conf.hash_algo;
> > > +    crypto_cfg.mac_algo_l = c->conf.mac_algo_l;
> > > +    crypto_cfg.mac_algo_h = c->conf.mac_algo_h;
> > > +    crypto_cfg.aead_algo = c->conf.aead_algo;
> > > +
> > > +    memcpy(config, &crypto_cfg, c->config_size);
> > >  }
> > 
> > What about endianness?  For example, if the host is big-endian then this
> > VIRTIO 1.0 device needs to byteswap multi-byte fields.  There is a
> > family of functions to help with this: virtio_stl_p().
> 
> I did that in v1. Michael told me that Virtio-1.0 devices are always little-endian, so
> I removed those helper functions in the following functions. But after this version,
> the virtio-crypto device isn't virtio-1.0 device by default, so I should use them again.

Endian awareness is still necessary for VIRTIO 1.0-only devices.  The
uint8_t *config data is just a blob.  Therefore nothing automatically
handles little-endian conversion for us.  We *must* read little-endian
from config[].
diff mbox

Patch

diff --git a/hw/virtio/virtio-crypto.c b/hw/virtio/virtio-crypto.c
index e639fb3..e74a15f 100644
--- a/hw/virtio/virtio-crypto.c
+++ b/hw/virtio/virtio-crypto.c
@@ -89,6 +89,22 @@  static void virtio_crypto_reset(VirtIODevice *vdev)
     }
 }
 
+static void virtio_crypto_init_config(VirtIODevice *vdev)
+{
+    VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
+
+    vcrypto->conf.crypto_services =
+                     vcrypto->conf.cryptodev->conf.crypto_services;
+    vcrypto->conf.cipher_algo_l =
+                     vcrypto->conf.cryptodev->conf.cipher_algo_l;
+    vcrypto->conf.cipher_algo_h =
+                     vcrypto->conf.cryptodev->conf.cipher_algo_h;
+    vcrypto->conf.hash_algo = vcrypto->conf.cryptodev->conf.hash_algo;
+    vcrypto->conf.mac_algo_l = vcrypto->conf.cryptodev->conf.mac_algo_l;
+    vcrypto->conf.mac_algo_h = vcrypto->conf.cryptodev->conf.mac_algo_h;
+    vcrypto->conf.aead_algo = vcrypto->conf.cryptodev->conf.aead_algo;
+}
+
 static void virtio_crypto_device_realize(DeviceState *dev, Error **errp)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
@@ -122,6 +138,7 @@  static void virtio_crypto_device_realize(DeviceState *dev, Error **errp)
     } else {
         vcrypto->status |= VIRTIO_CRYPTO_S_HW_READY;
     }
+    virtio_crypto_init_config(vdev);
     register_savevm(dev, "virtio-crypto", -1, 1, virtio_crypto_save,
                     virtio_crypto_load, vcrypto);
 }
@@ -142,7 +159,20 @@  static Property virtio_crypto_properties[] = {
 
 static void virtio_crypto_get_config(VirtIODevice *vdev, uint8_t *config)
 {
-
+    VirtIOCrypto *c = VIRTIO_CRYPTO(vdev);
+    struct virtio_crypto_config crypto_cfg;
+
+    crypto_cfg.status = c->status;
+    crypto_cfg.max_dataqueues = c->max_queues;
+    crypto_cfg.crypto_services = c->conf.crypto_services;
+    crypto_cfg.cipher_algo_l = c->conf.cipher_algo_l;
+    crypto_cfg.cipher_algo_h = c->conf.cipher_algo_h;
+    crypto_cfg.hash_algo = c->conf.hash_algo;
+    crypto_cfg.mac_algo_l = c->conf.mac_algo_l;
+    crypto_cfg.mac_algo_h = c->conf.mac_algo_h;
+    crypto_cfg.aead_algo = c->conf.aead_algo;
+
+    memcpy(config, &crypto_cfg, c->config_size);
 }
 
 static void virtio_crypto_set_config(VirtIODevice *vdev, const uint8_t *config)
diff --git a/include/hw/virtio/virtio-crypto.h b/include/hw/virtio/virtio-crypto.h
index 484062c..8aa2fe9 100644
--- a/include/hw/virtio/virtio-crypto.h
+++ b/include/hw/virtio/virtio-crypto.h
@@ -38,6 +38,20 @@  do { printf("virtio_crypto: " fmt , ## __VA_ARGS__); } while (0)
 
 typedef struct VirtIOCryptoConf {
     QCryptoCryptoDevBackend *cryptodev;
+
+    /* Supported service mask */
+    uint32_t crypto_services;
+
+    /* Detailed algorithms mask */
+    uint32_t cipher_algo_l;
+    uint32_t cipher_algo_h;
+    uint32_t hash_algo;
+    uint32_t mac_algo_l;
+    uint32_t mac_algo_h;
+    uint32_t asym_algo;
+    uint32_t kdf_algo;
+    uint32_t aead_algo;
+    uint32_t primitive_algo;
 } VirtIOCryptoConf;
 
 struct VirtIOCrypto;