瀏覽代碼

cryptodev: introduce an unified wrapper for crypto operation

We use an opaque point to the VirtIOCryptoReq which
can support different packets based on different
algorithms.

Signed-off-by: Gonglei <arei.gonglei@huawei.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Gonglei 8 年之前
父節點
當前提交
d6634ac09a
共有 3 個文件被更改,包括 38 次插入13 次删除
  1. 26 2
      backends/cryptodev.c
  2. 5 5
      hw/virtio/virtio-crypto.c
  3. 7 6
      include/sysemu/cryptodev.h

+ 26 - 2
backends/cryptodev.c

@@ -30,6 +30,8 @@
 #include "qapi-visit.h"
 #include "qemu/config-file.h"
 #include "qom/object_interfaces.h"
+#include "hw/virtio/virtio-crypto.h"
+
 
 static QTAILQ_HEAD(, CryptoDevBackendClient) crypto_clients;
 
@@ -105,7 +107,7 @@ int cryptodev_backend_sym_close_session(
     return -1;
 }
 
-int cryptodev_backend_sym_operation(
+static int cryptodev_backend_sym_operation(
                  CryptoDevBackend *backend,
                  CryptoDevBackendSymOpInfo *op_info,
                  uint32_t queue_index, Error **errp)
@@ -117,7 +119,29 @@ int cryptodev_backend_sym_operation(
         return bc->do_sym_op(backend, op_info, queue_index, errp);
     }
 
-    return -1;
+    return -VIRTIO_CRYPTO_ERR;
+}
+
+int cryptodev_backend_crypto_operation(
+                 CryptoDevBackend *backend,
+                 void *opaque,
+                 uint32_t queue_index, Error **errp)
+{
+    VirtIOCryptoReq *req = opaque;
+
+    if (req->flags == CRYPTODEV_BACKEND_ALG_SYM) {
+        CryptoDevBackendSymOpInfo *op_info;
+        op_info = req->u.sym_op_info;
+
+        return cryptodev_backend_sym_operation(backend,
+                         op_info, queue_index, errp);
+    } else {
+        error_setg(errp, "Unsupported cryptodev alg type: %" PRIu32 "",
+                   req->flags);
+       return -VIRTIO_CRYPTO_NOTSUPP;
+    }
+
+    return -VIRTIO_CRYPTO_ERR;
 }
 
 static void

+ 5 - 5
hw/virtio/virtio-crypto.c

@@ -634,15 +634,15 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request)
             /* Set request's parameter */
             request->flags = CRYPTODEV_BACKEND_ALG_SYM;
             request->u.sym_op_info = sym_op_info;
-            ret = cryptodev_backend_sym_operation(vcrypto->cryptodev,
-                                    sym_op_info, queue_index, &local_err);
+            ret = cryptodev_backend_crypto_operation(vcrypto->cryptodev,
+                                    request, queue_index, &local_err);
             if (ret < 0) {
-                status = VIRTIO_CRYPTO_ERR;
+                status = -ret;
                 if (local_err) {
                     error_report_err(local_err);
                 }
-            } else { /* ret >= 0 */
-                status = VIRTIO_CRYPTO_OK;
+            } else { /* ret == VIRTIO_CRYPTO_OK */
+                status = ret;
             }
             virtio_crypto_req_complete(request, status);
             virtio_crypto_free_request(request);

+ 7 - 6
include/sysemu/cryptodev.h

@@ -278,20 +278,21 @@ int cryptodev_backend_sym_close_session(
            uint32_t queue_index, Error **errp);
 
 /**
- * cryptodev_backend_sym_operation:
+ * cryptodev_backend_crypto_operation:
  * @backend: the cryptodev backend object
- * @op_info: parameters needed by symmetric crypto operation
+ * @opaque: pointer to a VirtIOCryptoReq object
  * @queue_index: queue index of cryptodev backend client
  * @errp: pointer to a NULL-initialized error object
  *
- * Do symmetric crypto operation, such as encryption and
+ * Do crypto operation, such as encryption and
  * decryption
  *
- * Returns: 0 on success, or Negative on error
+ * Returns: VIRTIO_CRYPTO_OK on success,
+ *         or -VIRTIO_CRYPTO_* on error
  */
-int cryptodev_backend_sym_operation(
+int cryptodev_backend_crypto_operation(
                  CryptoDevBackend *backend,
-                 CryptoDevBackendSymOpInfo *op_info,
+                 void *opaque,
                  uint32_t queue_index, Error **errp);
 
 #endif /* CRYPTODEV_H */