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