app/crypto-perf: parse segment size
[dpdk.git] / app / test-crypto-perf / cperf_test_common.c
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 segment_sz,
40                 uint32_t segments_nb,
41                 const struct cperf_options *options,
42                 const struct cperf_test_vector *test_vector)
43 {
44         struct rte_mbuf *mbuf;
45         uint8_t *mbuf_data;
46         uint8_t *test_data =
47                         (options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
48                                         test_vector->plaintext.data :
49                                         test_vector->ciphertext.data;
50         uint32_t remaining_bytes = options->max_buffer_size;
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         if (options->max_buffer_size <= segment_sz) {
61                 memcpy(mbuf_data, test_data, options->max_buffer_size);
62                 test_data += options->max_buffer_size;
63                 remaining_bytes = 0;
64         } else {
65                 memcpy(mbuf_data, test_data, segment_sz);
66                 test_data += segment_sz;
67                 remaining_bytes -= segment_sz;
68         }
69         segments_nb--;
70
71         while (remaining_bytes) {
72                 struct rte_mbuf *m;
73
74                 m = rte_pktmbuf_alloc(mempool);
75                 if (m == NULL)
76                         goto error;
77
78                 rte_pktmbuf_chain(mbuf, m);
79
80                 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
81                 if (mbuf_data == NULL)
82                         goto error;
83
84                 if (remaining_bytes <= segment_sz) {
85                         memcpy(mbuf_data, test_data, remaining_bytes);
86                         remaining_bytes = 0;
87                         test_data += remaining_bytes;
88                 } else {
89                         memcpy(mbuf_data, test_data, segment_sz);
90                         remaining_bytes -= segment_sz;
91                         test_data += segment_sz;
92                 }
93                 segments_nb--;
94         }
95
96         /*
97          * If there was not enough room for the digest at the end
98          * of the last segment, allocate a new one
99          */
100         if (segments_nb != 0) {
101                 struct rte_mbuf *m;
102                 m = rte_pktmbuf_alloc(mempool);
103
104                 if (m == NULL)
105                         goto error;
106
107                 rte_pktmbuf_chain(mbuf, m);
108                 mbuf_data = (uint8_t *)rte_pktmbuf_append(mbuf, segment_sz);
109                 if (mbuf_data == NULL)
110                         goto error;
111         }
112
113         return mbuf;
114 error:
115         if (mbuf != NULL)
116                 rte_pktmbuf_free(mbuf);
117
118         return NULL;
119 }
120
121 int
122 cperf_alloc_common_memory(const struct cperf_options *options,
123                         const struct cperf_test_vector *test_vector,
124                         uint8_t dev_id, size_t extra_op_priv_size,
125                         struct rte_mempool **pkt_mbuf_pool_in,
126                         struct rte_mempool **pkt_mbuf_pool_out,
127                         struct rte_mbuf ***mbufs_in,
128                         struct rte_mbuf ***mbufs_out,
129                         struct rte_mempool **crypto_op_pool)
130 {
131         unsigned int mbuf_idx = 0;
132         char pool_name[32] = "";
133
134         snprintf(pool_name, sizeof(pool_name), "cperf_pool_in_cdev_%d",
135                         dev_id);
136
137         uint32_t max_size = options->max_buffer_size + options->digest_sz;
138         uint16_t segments_nb = (max_size % options->segment_sz) ?
139                         (max_size / options->segment_sz) + 1 :
140                         max_size / options->segment_sz;
141
142         *pkt_mbuf_pool_in = rte_pktmbuf_pool_create(pool_name,
143                         options->pool_sz * segments_nb, 0, 0,
144                         RTE_PKTMBUF_HEADROOM + options->segment_sz,
145                         rte_socket_id());
146
147         if (*pkt_mbuf_pool_in == NULL)
148                 return -1;
149
150         /* Generate mbufs_in with plaintext populated for test */
151         *mbufs_in = (struct rte_mbuf **)rte_malloc(NULL,
152                         (sizeof(struct rte_mbuf *) * options->pool_sz), 0);
153
154         for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
155                 (*mbufs_in)[mbuf_idx] = cperf_mbuf_create(
156                                 *pkt_mbuf_pool_in,
157                                 options->segment_sz,
158                                 segments_nb,
159                                 options, test_vector);
160                 if ((*mbufs_in)[mbuf_idx] == NULL)
161                         return -1;
162         }
163
164         *mbufs_out = (struct rte_mbuf **)rte_zmalloc(NULL,
165                         (sizeof(struct rte_mbuf *) *
166                         options->pool_sz), 0);
167
168         if (options->out_of_place == 1) {
169                 snprintf(pool_name, sizeof(pool_name), "cperf_pool_out_cdev_%d",
170                                 dev_id);
171
172                 *pkt_mbuf_pool_out = rte_pktmbuf_pool_create(
173                                 pool_name, options->pool_sz, 0, 0,
174                                 RTE_PKTMBUF_HEADROOM + max_size,
175                                 rte_socket_id());
176
177                 if (*pkt_mbuf_pool_out == NULL)
178                         return -1;
179
180                 for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
181                         (*mbufs_out)[mbuf_idx] = cperf_mbuf_create(
182                                         *pkt_mbuf_pool_out, max_size,
183                                         1, options, test_vector);
184                         if ((*mbufs_out)[mbuf_idx] == NULL)
185                                 return -1;
186                 }
187         }
188
189         snprintf(pool_name, sizeof(pool_name), "cperf_op_pool_cdev_%d",
190                         dev_id);
191
192         uint16_t priv_size = RTE_ALIGN_CEIL(test_vector->cipher_iv.length +
193                 test_vector->auth_iv.length + test_vector->aead_iv.length +
194                 extra_op_priv_size, 16) +
195                 RTE_ALIGN_CEIL(options->aead_aad_sz, 16);
196
197         *crypto_op_pool = rte_crypto_op_pool_create(pool_name,
198                         RTE_CRYPTO_OP_TYPE_SYMMETRIC, options->pool_sz,
199                         512, priv_size, rte_socket_id());
200         if (*crypto_op_pool == NULL)
201                 return -1;
202
203         return 0;
204 }
205
206 void
207 cperf_free_common_memory(const struct cperf_options *options,
208                         struct rte_mempool *pkt_mbuf_pool_in,
209                         struct rte_mempool *pkt_mbuf_pool_out,
210                         struct rte_mbuf **mbufs_in,
211                         struct rte_mbuf **mbufs_out,
212                         struct rte_mempool *crypto_op_pool)
213 {
214         uint32_t i = 0;
215
216         if (mbufs_in) {
217                 while (mbufs_in[i] != NULL &&
218                                 i < options->pool_sz)
219                         rte_pktmbuf_free(mbufs_in[i++]);
220
221                 rte_free(mbufs_in);
222         }
223
224         if (mbufs_out) {
225                 i = 0;
226                 while (mbufs_out[i] != NULL
227                                 && i < options->pool_sz)
228                         rte_pktmbuf_free(mbufs_out[i++]);
229
230                 rte_free(mbufs_out);
231         }
232
233         if (pkt_mbuf_pool_in)
234                 rte_mempool_free(pkt_mbuf_pool_in);
235
236         if (pkt_mbuf_pool_out)
237                 rte_mempool_free(pkt_mbuf_pool_out);
238
239         if (crypto_op_pool)
240                 rte_mempool_free(crypto_op_pool);
241 }