app/crypto-perf: support multiple queue pairs
[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, uint16_t qp_id,
112                         size_t extra_op_priv_size,
113                         struct rte_mempool **pkt_mbuf_pool_in,
114                         struct rte_mempool **pkt_mbuf_pool_out,
115                         struct rte_mbuf ***mbufs_in,
116                         struct rte_mbuf ***mbufs_out,
117                         struct rte_mempool **crypto_op_pool)
118 {
119         unsigned int mbuf_idx = 0;
120         char pool_name[32] = "";
121
122         snprintf(pool_name, sizeof(pool_name), "cperf_pool_in_cdev_%u_qp_%u",
123                         dev_id, qp_id);
124
125         uint32_t max_size = options->max_buffer_size + options->digest_sz;
126         uint16_t segments_nb = (max_size % options->segment_sz) ?
127                         (max_size / options->segment_sz) + 1 :
128                         max_size / options->segment_sz;
129
130         *pkt_mbuf_pool_in = rte_pktmbuf_pool_create(pool_name,
131                         options->pool_sz * segments_nb, 0, 0,
132                         RTE_PKTMBUF_HEADROOM + options->segment_sz,
133                         rte_socket_id());
134
135         if (*pkt_mbuf_pool_in == NULL)
136                 return -1;
137
138         /* Generate mbufs_in with plaintext populated for test */
139         *mbufs_in = (struct rte_mbuf **)rte_malloc(NULL,
140                         (sizeof(struct rte_mbuf *) * options->pool_sz), 0);
141
142         for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
143                 (*mbufs_in)[mbuf_idx] = cperf_mbuf_create(
144                                 *pkt_mbuf_pool_in,
145                                 options->segment_sz,
146                                 segments_nb,
147                                 options);
148                 if ((*mbufs_in)[mbuf_idx] == NULL)
149                         return -1;
150         }
151
152         *mbufs_out = (struct rte_mbuf **)rte_zmalloc(NULL,
153                         (sizeof(struct rte_mbuf *) *
154                         options->pool_sz), 0);
155
156         if (options->out_of_place == 1) {
157                 snprintf(pool_name, sizeof(pool_name), "cperf_pool_out_cdev_%u_qp_%u",
158                                 dev_id, qp_id);
159
160                 *pkt_mbuf_pool_out = rte_pktmbuf_pool_create(
161                                 pool_name, options->pool_sz, 0, 0,
162                                 RTE_PKTMBUF_HEADROOM + max_size,
163                                 rte_socket_id());
164
165                 if (*pkt_mbuf_pool_out == NULL)
166                         return -1;
167
168                 for (mbuf_idx = 0; mbuf_idx < options->pool_sz; mbuf_idx++) {
169                         (*mbufs_out)[mbuf_idx] = cperf_mbuf_create(
170                                         *pkt_mbuf_pool_out, max_size,
171                                         1, options);
172                         if ((*mbufs_out)[mbuf_idx] == NULL)
173                                 return -1;
174                 }
175         }
176
177         snprintf(pool_name, sizeof(pool_name), "cperf_op_pool_cdev_%u_qp_%u",
178                         dev_id, qp_id);
179
180         uint16_t priv_size = RTE_ALIGN_CEIL(test_vector->cipher_iv.length +
181                 test_vector->auth_iv.length + test_vector->aead_iv.length +
182                 extra_op_priv_size, 16) +
183                 RTE_ALIGN_CEIL(options->aead_aad_sz, 16);
184
185         *crypto_op_pool = rte_crypto_op_pool_create(pool_name,
186                         RTE_CRYPTO_OP_TYPE_SYMMETRIC, options->pool_sz,
187                         512, priv_size, rte_socket_id());
188         if (*crypto_op_pool == NULL)
189                 return -1;
190
191         return 0;
192 }
193
194 void
195 cperf_free_common_memory(const struct cperf_options *options,
196                         struct rte_mempool *pkt_mbuf_pool_in,
197                         struct rte_mempool *pkt_mbuf_pool_out,
198                         struct rte_mbuf **mbufs_in,
199                         struct rte_mbuf **mbufs_out,
200                         struct rte_mempool *crypto_op_pool)
201 {
202         uint32_t i = 0;
203
204         if (mbufs_in) {
205                 while (mbufs_in[i] != NULL &&
206                                 i < options->pool_sz)
207                         rte_pktmbuf_free(mbufs_in[i++]);
208
209                 rte_free(mbufs_in);
210         }
211
212         if (mbufs_out) {
213                 i = 0;
214                 while (mbufs_out[i] != NULL
215                                 && i < options->pool_sz)
216                         rte_pktmbuf_free(mbufs_out[i++]);
217
218                 rte_free(mbufs_out);
219         }
220
221         if (pkt_mbuf_pool_in)
222                 rte_mempool_free(pkt_mbuf_pool_in);
223
224         if (pkt_mbuf_pool_out)
225                 rte_mempool_free(pkt_mbuf_pool_out);
226
227         if (crypto_op_pool)
228                 rte_mempool_free(crypto_op_pool);
229 }