xref: /dpdk/app/test-crypto-perf/cperf_test_common.c (revision 253624f46c9d34e6970ffa0dd709bb30399547fd)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <rte_malloc.h>
34 
35 #include "cperf_test_common.h"
36 
37 static struct rte_mbuf *
38 cperf_mbuf_create(struct rte_mempool *mempool,
39 		uint32_t segments_nb,
40 		const struct cperf_options *options,
41 		const struct cperf_test_vector *test_vector)
42 {
43 	struct rte_mbuf *mbuf;
44 	uint32_t segment_sz = options->max_buffer_size / segments_nb;
45 	uint32_t last_sz = options->max_buffer_size % segments_nb;
46 	uint8_t *mbuf_data;
47 	uint8_t *test_data =
48 			(options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
49 					test_vector->plaintext.data :
50 					test_vector->ciphertext.data;
51 
52 	mbuf = rte_pktmbuf_alloc(mempool);
53 	if (mbuf == NULL)
54 		goto error;
55 
56 	mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
57 	if (mbuf_data == NULL)
58 		goto error;
59 
60 	memcpy(mbuf_data, test_data, segment_sz);
61 	test_data += segment_sz;
62 	segments_nb--;
63 
64 	while (segments_nb) {
65 		struct rte_mbuf *m;
66 
67 		m = rte_pktmbuf_alloc(mempool);
68 		if (m == NULL)
69 			goto error;
70 
71 		rte_pktmbuf_chain(mbuf, m);
72 
73 		mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
74 		if (mbuf_data == NULL)
75 			goto error;
76 
77 		memcpy(mbuf_data, test_data, segment_sz);
78 		test_data += segment_sz;
79 		segments_nb--;
80 	}
81 
82 	if (last_sz) {
83 		mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, last_sz);
84 		if (mbuf_data == NULL)
85 			goto error;
86 
87 		memcpy(mbuf_data, test_data, last_sz);
88 	}
89 
90 	if (options->op_type != CPERF_CIPHER_ONLY) {
91 		mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf,
92 				options->digest_sz);
93 		if (mbuf_data == NULL)
94 			goto error;
95 	}
96 
97 	if (options->op_type == CPERF_AEAD) {
98 		uint8_t *aead = (uint8_t *)rte_pktmbuf_prepend(mbuf,
99 			RTE_ALIGN_CEIL(options->aead_aad_sz, 16));
100 
101 		if (aead == NULL)
102 			goto error;
103 
104 		memcpy(aead, test_vector->aad.data, test_vector->aad.length);
105 	}
106 
107 	return mbuf;
108 error:
109 	if (mbuf != NULL)
110 		rte_pktmbuf_free(mbuf);
111 
112 	return NULL;
113 }
114 
115 int
116 cperf_alloc_common_memory(const struct cperf_options *options,
117 			const struct cperf_test_vector *test_vector,
118 			uint8_t dev_id, size_t extra_op_priv_size,
119 			struct rte_mempool **pkt_mbuf_pool_in,
120 			struct rte_mempool **pkt_mbuf_pool_out,
121 			struct rte_mbuf ***mbufs_in,
122 			struct rte_mbuf ***mbufs_out,
123 			struct rte_mempool **crypto_op_pool)
124 {
125 	unsigned int mbuf_idx = 0;
126 	char pool_name[32] = "";
127 
128 	snprintf(pool_name, sizeof(pool_name), "cperf_pool_in_cdev_%d",
129 			dev_id);
130 
131 	*pkt_mbuf_pool_in = rte_pktmbuf_pool_create(pool_name,
132 			options->pool_sz * options->segments_nb, 0, 0,
133 			RTE_PKTMBUF_HEADROOM +
134 			RTE_CACHE_LINE_ROUNDUP(
135 				(options->max_buffer_size / options->segments_nb) +
136 				(options->max_buffer_size % options->segments_nb) +
137 					options->digest_sz),
138 			rte_socket_id());
139 
140 	if (*pkt_mbuf_pool_in == NULL)
141 		return -1;
142 
143 	/* Generate mbufs_in with plaintext populated for test */
144 	*mbufs_in = (struct rte_mbuf **)rte_malloc(NULL,
145 			(sizeof(struct rte_mbuf *) * options->pool_sz), 0);
146 
147 	for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
148 		(*mbufs_in)[mbuf_idx] = cperf_mbuf_create(
149 				*pkt_mbuf_pool_in, options->segments_nb,
150 				options, test_vector);
151 		if ((*mbufs_in)[mbuf_idx] == NULL)
152 			return -1;
153 	}
154 
155 	*mbufs_out = (struct rte_mbuf **)rte_zmalloc(NULL,
156 			(sizeof(struct rte_mbuf *) *
157 			options->pool_sz), 0);
158 
159 	if (options->out_of_place == 1)	{
160 		snprintf(pool_name, sizeof(pool_name), "cperf_pool_out_cdev_%d",
161 				dev_id);
162 
163 		*pkt_mbuf_pool_out = rte_pktmbuf_pool_create(
164 				pool_name, options->pool_sz, 0, 0,
165 				RTE_PKTMBUF_HEADROOM +
166 				RTE_CACHE_LINE_ROUNDUP(
167 					options->max_buffer_size +
168 					options->digest_sz),
169 				rte_socket_id());
170 
171 		if (*pkt_mbuf_pool_out == NULL)
172 			return -1;
173 
174 		for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
175 			(*mbufs_out)[mbuf_idx] = cperf_mbuf_create(
176 					*pkt_mbuf_pool_out, 1,
177 					options, test_vector);
178 			if ((*mbufs_out)[mbuf_idx] == NULL)
179 				return -1;
180 		}
181 	}
182 
183 	snprintf(pool_name, sizeof(pool_name), "cperf_op_pool_cdev_%d",
184 			dev_id);
185 
186 	uint16_t priv_size = test_vector->cipher_iv.length +
187 		test_vector->auth_iv.length + test_vector->aead_iv.length +
188 		extra_op_priv_size;
189 
190 	*crypto_op_pool = rte_crypto_op_pool_create(pool_name,
191 			RTE_CRYPTO_OP_TYPE_SYMMETRIC, options->pool_sz,
192 			512, priv_size, rte_socket_id());
193 	if (*crypto_op_pool == NULL)
194 		return -1;
195 
196 	return 0;
197 }
198 
199 void
200 cperf_free_common_memory(const struct cperf_options *options,
201 			struct rte_mempool *pkt_mbuf_pool_in,
202 			struct rte_mempool *pkt_mbuf_pool_out,
203 			struct rte_mbuf **mbufs_in,
204 			struct rte_mbuf **mbufs_out,
205 			struct rte_mempool *crypto_op_pool)
206 {
207 	uint32_t i = 0;
208 
209 	if (mbufs_in) {
210 		while (mbufs_in[i] != NULL &&
211 				i < options->pool_sz)
212 			rte_pktmbuf_free(mbufs_in[i++]);
213 
214 		rte_free(mbufs_in);
215 	}
216 
217 	if (mbufs_out) {
218 		i = 0;
219 		while (mbufs_out[i] != NULL
220 				&& i < options->pool_sz)
221 			rte_pktmbuf_free(mbufs_out[i++]);
222 
223 		rte_free(mbufs_out);
224 	}
225 
226 	if (pkt_mbuf_pool_in)
227 		rte_mempool_free(pkt_mbuf_pool_in);
228 
229 	if (pkt_mbuf_pool_out)
230 		rte_mempool_free(pkt_mbuf_pool_out);
231 
232 	if (crypto_op_pool)
233 		rte_mempool_free(crypto_op_pool);
234 }
235