Skip to contentSkip to conversation

examples/vhost_crypto: fix zero copy

PATCH Superseded Fan Zhang 4 participants 7 messages Oct 30, 2018 – Nov 16, 2018

lore
Share & tools
lore

Fan Zhang

author
#0 lore raw

PATCH 1/1 examples/vhost_crypto: fix zero copy

This patch fixes the zero copy enable problem for vhost crypto sample application.

For some Crypto PMDs such as AESNI-MB and AESNI-GCM the data to be processed will be made a copy in the same buffer but next to the data. For example, to encrypt 64 bytes data the PMD will copy this data from offset 64 to offset 123. This requires the application provides the buffer with at least double of the data size.

However there is no way for VMs to know this limitation. When zero-copy is enabled in Vhost the PMD may overwrite the buffer next to the VM data to be processed, and further cause problems such as Segmentation Fault or even worse, crashes the VM.

To fix the problem the user should avoid enabling the zero copy for these Crypto PMDs. This patch adds the checking of the PMD names to see if zero copy can be applied.

Fixes 709521f4c2cd ("examples/vhost_crypto: support multi-core")
---
 examples/vhost_crypto/main.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
examples/vhost_crypto/main.c +21 -2
index cbb5e49d2..887e3eb6f 100644
@@ -4,6 +4,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include <stdbool.h>
 #include <assert.h>
@@ -442,8 +443,13 @@ free_resource(void)
 		struct lcore_option *lo = &options.los[i];
 		struct vhost_crypto_info *info = options.infos[i];
 
-		rte_mempool_free(info->cop_pool);
-		rte_mempool_free(info->sess_pool);
+		if (!info)
+			continue;
+
+		if (info->cop_pool)
+			rte_mempool_free(info->cop_pool);
+		if (info->sess_pool)
+			rte_mempool_free(info->sess_pool);
 
 		for (j = 0; j < lo->nb_sockets; j++) {
 			rte_vhost_driver_unregister(lo->socket_files[i]);
@@ -493,6 +499,19 @@ main(int argc, char *argv[])
 		info->nb_vids = lo->nb_sockets;
 
 		rte_cryptodev_info_get(info->cid, &dev_info);
+		if (options.zero_copy == RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE) {
+#define VHOST_CRYPTO_CDEV_NAME_AESNI_MB_PMD	crypto_aesni_mb
+#define VHOST_CRYPTO_CDEV_NAME_AESNI_GCM_PMD	crypto_aesni_gcm
+			if (strstr(dev_info.driver_name,
+				RTE_STR(VHOST_CRYPTO_CDEV_NAME_AESNI_MB_PMD)) ||
+				strstr(dev_info.driver_name,
+				RTE_STR(VHOST_CRYPTO_CDEV_NAME_AESNI_GCM_PMD)))
+			RTE_LOG(ERR, USER1, "Cannot enable Zero Copy to %s\n",
+					dev_info.driver_name);
+			ret = -EPERM;
+			goto error_exit;
+		}
+
 		if (dev_info.max_nb_queue_pairs < info->qid + 1) {
 			RTE_LOG(ERR, USER1, "Number of queues cannot over %u",
 					dev_info.max_nb_queue_pairs);
Signature · 1 line
2.13.6
#1 lore raw
Quoted 41 lines · Fan Zhang

This patch fixes the zero copy enable problem for vhost crypto sample application.

For some Crypto PMDs such as AESNI-MB and AESNI-GCM the data to be processed will be made a copy in the same buffer but next to the data. For example, to encrypt 64 bytes data the PMD will copy this data from offset 64 to offset 123. This requires the application provides the buffer with at least double of the data size.

However there is no way for VMs to know this limitation. When zero-copy is enabled in Vhost the PMD may overwrite the buffer next to the VM data to be processed, and further cause problems such as Segmentation Fault or even worse, crashes the VM.

To fix the problem the user should avoid enabling the zero copy for these Crypto PMDs. This patch adds the checking of the PMD names to see if zero copy can be applied.

Fixes 709521f4c2cd ("examples/vhost_crypto: support multi-core")
---
  examples/vhost_crypto/main.c | 23 +++++++++++++++++++++--
  1 file changed, 21 insertions(+), 2 deletions(-)
examples/vhost_crypto/main.c +1 -0
index cbb5e49d2..887e3eb6f 100644
@@ -4,6 +4,7 @@
 
  #include <stdio.h>
  #include <stdlib.h>
+#include <string.h>
  #include <unistd.h>
  #include <stdbool.h>
  #include <assert.h>
@@ -442,8 +443,13 @@ free_resource(void)
  		struct lcore_option *lo = &options.los[i];
  		struct vhost_crypto_info *info = options.infos[i];
 

examples/vhost_crypto/main.c free_resource(void) · line 453

Quoted 10 lines
examples/vhost_crypto/main.c +7 -2
-		rte_mempool_free(info->cop_pool);
-		rte_mempool_free(info->sess_pool);
+		if (!info)
+			continue;
+
+		if (info->cop_pool)
+			rte_mempool_free(info->cop_pool);
+		if (info->sess_pool)
+			rte_mempool_free(info->sess_pool);
 

rte_mempool_free() already does a NULL-check (as per libc free() convention), and if you are to do a NULL-check it should be an explicit one ("!= NULL").

examples/vhost_crypto/main.c main(int argc, char *argv[]) · line 504

Quoted 9 lines
examples/vhost_crypto/main.c +3 -0
  		for (j = 0; j < lo->nb_sockets; j++) {
  			rte_vhost_driver_unregister(lo->socket_files[i]);
@@ -493,6 +499,19 @@ main(int argc, char *argv[])
  		info->nb_vids = lo->nb_sockets;
 
  		rte_cryptodev_info_get(info->cid, &dev_info);
+		if (options.zero_copy == RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE) {
+#define VHOST_CRYPTO_CDEV_NAME_AESNI_MB_PMD	crypto_aesni_mb
+#define VHOST_CRYPTO_CDEV_NAME_AESNI_GCM_PMD	crypto_aesni_gcm

What's the purpose of these defines?

examples/vhost_crypto/main.c main(int argc, char *argv[]) · line 510

Quoted 6 lines
examples/vhost_crypto/main.c +6 -0
+			if (strstr(dev_info.driver_name,
+				RTE_STR(VHOST_CRYPTO_CDEV_NAME_AESNI_MB_PMD)) ||
+				strstr(dev_info.driver_name,
+				RTE_STR(VHOST_CRYPTO_CDEV_NAME_AESNI_GCM_PMD)))
+			RTE_LOG(ERR, USER1, "Cannot enable Zero Copy to %s\n",
+					dev_info.driver_name);

"Zero Copy to" should probably be "zero-copy in" or "Zero-copy in".

#2 lore raw

Hi Fan,

Could you please have a look at Mattias comments and reply?

Thanks in advance,
Maxime

Quoted 71 lines · Mattias Rönnblom
Quoted 38 lines · Fan Zhang

This patch fixes the zero copy enable problem for vhost crypto sample application.

For some Crypto PMDs such as AESNI-MB and AESNI-GCM the data to be processed will be made a copy in the same buffer but next to the data. For example, to encrypt 64 bytes data the PMD will copy this data from offset 64 to offset 123. This requires the application provides the buffer with at least double of the data size.

However there is no way for VMs to know this limitation. When zero-copy is enabled in Vhost the PMD may overwrite the buffer next to the VM data to be processed, and further cause problems such as Segmentation Fault or even worse, crashes the VM.

To fix the problem the user should avoid enabling the zero copy for these Crypto PMDs. This patch adds the checking of the PMD names to see if zero copy can be applied.

Fixes 709521f4c2cd ("examples/vhost_crypto: support multi-core")

---
  examples/vhost_crypto/main.c | 23 +++++++++++++++++++++--

  1 file changed, 21 insertions(+), 2 deletions(-)
examples/vhost_crypto/main.c +1 -0
index cbb5e49d2..887e3eb6f 100644
@@ -4,6 +4,7 @@
   #include <stdio.h>
   #include <stdlib.h>
+#include <string.h>
   #include <unistd.h>
   #include <stdbool.h>
   #include <assert.h>
@@ -442,8 +443,13 @@ free_resource(void)
           struct lcore_option *lo = &options.los[i];

examples/vhost_crypto/main.c free_resource(void) · line 451

Quoted 10 lines
examples/vhost_crypto/main.c +7 -2
           struct vhost_crypto_info *info = options.infos[i];
-        rte_mempool_free(info->cop_pool);
-        rte_mempool_free(info->sess_pool);
+        if (!info)
+            continue;
+
+        if (info->cop_pool)
+            rte_mempool_free(info->cop_pool);
+        if (info->sess_pool)
+            rte_mempool_free(info->sess_pool);

rte_mempool_free() already does a NULL-check (as per libc free() convention), and if you are to do a NULL-check it should be an explicit one ("!= NULL").

examples/vhost_crypto/main.c main(int argc, char *argv[]) · line 503

Quoted 8 lines
examples/vhost_crypto/main.c +3 -0
           for (j = 0; j < lo->nb_sockets; j++) {
               rte_vhost_driver_unregister(lo->socket_files[i]);
@@ -493,6 +499,19 @@ main(int argc, char *argv[])
           info->nb_vids = lo->nb_sockets;
           rte_cryptodev_info_get(info->cid, &dev_info);
+        if (options.zero_copy == RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE) {
+#define VHOST_CRYPTO_CDEV_NAME_AESNI_MB_PMD    crypto_aesni_mb
+#define VHOST_CRYPTO_CDEV_NAME_AESNI_GCM_PMD    crypto_aesni_gcm

What's the purpose of these defines?

examples/vhost_crypto/main.c main(int argc, char *argv[]) · line 510

Quoted 6 lines
examples/vhost_crypto/main.c +6 -0
+            if (strstr(dev_info.driver_name,
+                RTE_STR(VHOST_CRYPTO_CDEV_NAME_AESNI_MB_PMD)) ||
+                strstr(dev_info.driver_name,
+                RTE_STR(VHOST_CRYPTO_CDEV_NAME_AESNI_GCM_PMD)))
+            RTE_LOG(ERR, USER1, "Cannot enable Zero Copy to %s\n",
+                    dev_info.driver_name);

"Zero Copy to" should probably be "zero-copy in" or "Zero-copy in".

#3 lore raw
In reply to Mattias Rönnblom

Hi Mattias,

Sorry for the late reply. Comments inline.

Quoted 7 lines

-----Original Message-----
From: Mattias Rönnblom [mailto:[email protected]]
Sent: Tuesday, October 30, 2018 7:38 PM
To: Zhang, Roy Fan <[email protected]>; [email protected]
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH] examples/vhost_crypto: fix zero copy

Quoted 6 lines
Quoted 2 lines · Fan Zhang

This patch fixes the zero copy enable problem for vhost crypto sample application.

rte_mempool_free() already does a NULL-check (as per libc free() convention), and if you are to do a NULL-check it should be an explicit one ("!= NULL").

No problem, I can change that.

Quoted 9 lines

examples/vhost_crypto/main.c main(int argc, char *argv[]) · line 502

Quoted 8 lines
  		for (j = 0; j < lo->nb_sockets; j++) {
  			rte_vhost_driver_unregister(lo->socket_files[i]);
examples/vhost_crypto/main.c +1 -0
@@ -493,6 +499,19 @@ main(int argc, char *argv[])
  		info->nb_vids = lo->nb_sockets;
 
  		rte_cryptodev_info_get(info->cid, &dev_info);
+		if (options.zero_copy ==

RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE) {

Quoted 6 lines

Review main(int argc, char *argv[]) · line 503

Quoted 1 line
(quoted diff) +1 -0
+#define VHOST_CRYPTO_CDEV_NAME_AESNI_MB_PMD

crypto_aesni_mb

Review main(int argc, char *argv[]) · line 504

Quoted 1 line
(quoted diff) +1 -0
+#define VHOST_CRYPTO_CDEV_NAME_AESNI_GCM_PMD

crypto_aesni_gcm

What's the purpose of these defines?

These two PMD names are defined inside the PMDs and are not exposed to the vhost library.
Also these two PMDs are the ones that copy to the end of the packet for computation purposes, which will cause the problem for virtio case.

Quoted 4 lines

examples/vhost_crypto/main.c main(int argc, char *argv[]) · line 514

Quoted 3 lines
examples/vhost_crypto/main.c +2 -0
+			if (strstr(dev_info.driver_name,
+
	RTE_STR(VHOST_CRYPTO_CDEV_NAME_AESNI_MB_PMD)) ||
Quoted 8 lines

examples/vhost_crypto/main.c main(int argc, char *argv[]) · line 514

Quoted 2 lines
examples/vhost_crypto/main.c +2 -0
+				strstr(dev_info.driver_name,
+

RTE_STR(VHOST_CRYPTO_CDEV_NAME_AESNI_GCM_PMD)))

Review main(int argc, char *argv[]) · line 509

Quoted 1 line
(quoted diff) +1 -0
+			RTE_LOG(ERR, USER1, "Cannot enable Zero Copy

to %s\n",

examples/vhost_crypto/main.c main(int argc, char *argv[]) · line 510

Quoted 1 line
examples/vhost_crypto/main.c +1 -0
+					dev_info.driver_name);

"Zero Copy to" should probably be "zero-copy in" or "Zero-copy in".

Thanks I will change that.

Regards,
Fan

author
#4 lore raw

PATCH v2 1/1 examples/vhost_crypto: fix zero copy

In reply to Fan Zhang

This patch fixes the zero copy enable problem for vhost crypto sample application.

For some Crypto PMDs such as AESNI-MB and AESNI-GCM the data to be processed will be made a copy in the same buffer but next to the data. For example, to encrypt 64 bytes data the PMD will copy this data from offset 64 to offset 123. This requires the application provides the buffer with at least double of the data size.

However there is no way for VMs to know this limitation. When zero-copy is enabled in Vhost the PMD may overwrite the buffer next to the VM data to be processed, and further cause problems such as Segmentation Fault or even worse, crashes the VM.

To fix the problem the user should avoid enabling the zero copy for these Crypto PMDs. This patch adds the checking of the PMD names to see if zero copy can be applied.

Fixes 709521f4c2cd ("examples/vhost_crypto: support multi-core")

---
v2:
- removed unecessary checks
- Changed log message when zero-copy is not applicable.

 examples/vhost_crypto/main.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
examples/vhost_crypto/main.c +17 -0
index cbb5e49d2..f08babd97 100644
@@ -4,6 +4,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include <stdbool.h>
 #include <assert.h>
@@ -442,6 +443,9 @@ free_resource(void)
 		struct lcore_option *lo = &options.los[i];
 		struct vhost_crypto_info *info = options.infos[i];
 
+		if (!info)
+			continue;
+
 		rte_mempool_free(info->cop_pool);
 		rte_mempool_free(info->sess_pool);
 
@@ -493,6 +497,19 @@ main(int argc, char *argv[])
 		info->nb_vids = lo->nb_sockets;
 
 		rte_cryptodev_info_get(info->cid, &dev_info);
+		if (options.zero_copy == RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE) {
+#define VHOST_CRYPTO_CDEV_NAME_AESNI_MB_PMD	crypto_aesni_mb
+#define VHOST_CRYPTO_CDEV_NAME_AESNI_GCM_PMD	crypto_aesni_gcm
+			if (strstr(dev_info.driver_name,
+				RTE_STR(VHOST_CRYPTO_CDEV_NAME_AESNI_MB_PMD)) ||
+				strstr(dev_info.driver_name,
+				RTE_STR(VHOST_CRYPTO_CDEV_NAME_AESNI_GCM_PMD)))
+			RTE_LOG(ERR, USER1, "Cannot enable zero-copy in %s\n",
+					dev_info.driver_name);
+			ret = -EPERM;
+			goto error_exit;
+		}
+
 		if (dev_info.max_nb_queue_pairs < info->qid + 1) {
 			RTE_LOG(ERR, USER1, "Number of queues cannot over %u",
 					dev_info.max_nb_queue_pairs);
Signature · 1 line
2.13.6
Reviewed-by on v2 1/1
#5 lore raw
Quoted 20 lines · Fan Zhang

This patch fixes the zero copy enable problem for vhost crypto sample application.

For some Crypto PMDs such as AESNI-MB and AESNI-GCM the data to be processed will be made a copy in the same buffer but next to the data. For example, to encrypt 64 bytes data the PMD will copy this data from offset 64 to offset 123. This requires the application provides the buffer with at least double of the data size.

However there is no way for VMs to know this limitation. When zero-copy is enabled in Vhost the PMD may overwrite the buffer next to the VM data to be processed, and further cause problems such as Segmentation Fault or even worse, crashes the VM.

To fix the problem the user should avoid enabling the zero copy for these Crypto PMDs. This patch adds the checking of the PMD names to see if zero copy can be applied.

Fixes 709521f4c2cd ("examples/vhost_crypto: support multi-core")
Quoted 9 lines

---
v2:
- removed unecessary checks
- Changed log message when zero-copy is not applicable.

  examples/vhost_crypto/main.c | 17 +++++++++++++++++
  1 file changed, 17 insertions(+)

Thanks!
Maxime

Applied
#6 lore raw
Quoted 29 lines · Maxime Coquelin
Quoted 26 lines · Fan Zhang

This patch fixes the zero copy enable problem for vhost crypto sample application.

For some Crypto PMDs such as AESNI-MB and AESNI-GCM the data to be processed will be made a copy in the same buffer but next to the data. For example, to encrypt 64 bytes data the PMD will copy this data from offset 64 to offset 123. This requires the application provides the buffer with at least double of the data size.

However there is no way for VMs to know this limitation. When zero-copy is enabled in Vhost the PMD may overwrite the buffer next to the VM data to be processed, and further cause problems such as Segmentation Fault or even worse, crashes the VM.

To fix the problem the user should avoid enabling the zero copy for these Crypto PMDs. This patch adds the checking of the PMD names to see if zero copy can be applied.

Fixes 709521f4c2cd ("examples/vhost_crypto: support multi-core")

---
v2:
- removed unecessary checks
- Changed log message when zero-copy is not applicable.

Quoted 5 lines
Quoted 3 lines
  examples/vhost_crypto/main.c | 17 +++++++++++++++++
  1 file changed, 17 insertions(+)

Applied to dpdk-next-net/master, thanks.