New upstream version 17.11-rc3
[deb_dpdk.git] / test / test / test_cryptodev.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015-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 #ifndef TEST_CRYPTODEV_H_
33 #define TEST_CRYPTODEV_H_
34
35 #define HEX_DUMP 0
36
37 #define FALSE                           0
38 #define TRUE                            1
39
40 #define MAX_NUM_OPS_INFLIGHT            (4096)
41 #define MIN_NUM_OPS_INFLIGHT            (128)
42 #define DEFAULT_NUM_OPS_INFLIGHT        (128)
43
44 #define MAX_NUM_QPS_PER_QAT_DEVICE      (2)
45 #define DEFAULT_NUM_QPS_PER_QAT_DEVICE  (2)
46 #define DEFAULT_BURST_SIZE              (64)
47 #define DEFAULT_NUM_XFORMS              (2)
48 #define NUM_MBUFS                       (8191)
49 #define MBUF_CACHE_SIZE                 (256)
50 #define MBUF_DATAPAYLOAD_SIZE           (2048 + DIGEST_BYTE_LENGTH_SHA512)
51 #define MBUF_SIZE                       (sizeof(struct rte_mbuf) + \
52                 RTE_PKTMBUF_HEADROOM + MBUF_DATAPAYLOAD_SIZE)
53
54 #define BYTE_LENGTH(x)                          (x/8)
55 /* HASH DIGEST LENGTHS */
56 #define DIGEST_BYTE_LENGTH_MD5                  (BYTE_LENGTH(128))
57 #define DIGEST_BYTE_LENGTH_SHA1                 (BYTE_LENGTH(160))
58 #define DIGEST_BYTE_LENGTH_SHA224               (BYTE_LENGTH(224))
59 #define DIGEST_BYTE_LENGTH_SHA256               (BYTE_LENGTH(256))
60 #define DIGEST_BYTE_LENGTH_SHA384               (BYTE_LENGTH(384))
61 #define DIGEST_BYTE_LENGTH_SHA512               (BYTE_LENGTH(512))
62 #define DIGEST_BYTE_LENGTH_AES_XCBC             (BYTE_LENGTH(96))
63 #define DIGEST_BYTE_LENGTH_SNOW3G_UIA2          (BYTE_LENGTH(32))
64 #define DIGEST_BYTE_LENGTH_KASUMI_F9            (BYTE_LENGTH(32))
65 #define AES_XCBC_MAC_KEY_SZ                     (16)
66 #define DIGEST_BYTE_LENGTH_AES_GCM              (BYTE_LENGTH(128))
67
68 #define TRUNCATED_DIGEST_BYTE_LENGTH_SHA1               (12)
69 #define TRUNCATED_DIGEST_BYTE_LENGTH_SHA224             (16)
70 #define TRUNCATED_DIGEST_BYTE_LENGTH_SHA256             (16)
71 #define TRUNCATED_DIGEST_BYTE_LENGTH_SHA384             (24)
72 #define TRUNCATED_DIGEST_BYTE_LENGTH_SHA512             (32)
73
74 #define MAXIMUM_IV_LENGTH                               (16)
75
76 #define IV_OFFSET                       (sizeof(struct rte_crypto_op) + \
77                 sizeof(struct rte_crypto_sym_op) + DEFAULT_NUM_XFORMS * \
78                 sizeof(struct rte_crypto_sym_xform))
79
80 #define CRYPTODEV_NAME_NULL_PMD         crypto_null
81 #define CRYPTODEV_NAME_AESNI_MB_PMD     crypto_aesni_mb
82 #define CRYPTODEV_NAME_AESNI_GCM_PMD    crypto_aesni_gcm
83 #define CRYPTODEV_NAME_OPENSSL_PMD      crypto_openssl
84 #define CRYPTODEV_NAME_QAT_SYM_PMD      crypto_qat
85 #define CRYPTODEV_NAME_SNOW3G_PMD       crypto_snow3g
86 #define CRYPTODEV_NAME_KASUMI_PMD       crypto_kasumi
87 #define CRYPTODEV_NAME_ZUC_PMD          crypto_zuc
88 #define CRYPTODEV_NAME_ARMV8_PMD        crypto_armv8
89 #define CRYPTODEV_NAME_DPAA2_SEC_PMD    crypto_dpaa2_sec
90 #define CRYPTODEV_NAME_SCHEDULER_PMD    crypto_scheduler
91 #define CRYPTODEV_NAME_MRVL_PMD         crypto_mrvl
92
93 /**
94  * Write (spread) data from buffer to mbuf data
95  *
96  * @param mbuf
97  *   Destination mbuf
98  * @param offset
99  *   Start offset in mbuf
100  * @param len
101  *   Number of bytes to copy
102  * @param buffer
103  *   Continuous source buffer
104  */
105 static inline void
106 pktmbuf_write(struct rte_mbuf *mbuf, int offset, int len, const uint8_t *buffer)
107 {
108         int n = len;
109         int l;
110         struct rte_mbuf *m;
111         char *dst;
112
113         for (m = mbuf; (m != NULL) && (offset > m->data_len); m = m->next)
114                 offset -= m->data_len;
115
116         l = m->data_len - offset;
117
118         /* copy data from first segment */
119         dst = rte_pktmbuf_mtod_offset(m, char *, offset);
120         if (len <= l) {
121                 rte_memcpy(dst, buffer, len);
122                 return;
123         }
124
125         rte_memcpy(dst, buffer, l);
126         buffer += l;
127         n -= l;
128
129         for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
130                 dst = rte_pktmbuf_mtod(m, char *);
131                 l = m->data_len;
132                 if (n < l) {
133                         rte_memcpy(dst, buffer, n);
134                         return;
135                 }
136                 rte_memcpy(dst, buffer, l);
137                 buffer += l;
138                 n -= l;
139         }
140 }
141
142 static inline uint8_t *
143 pktmbuf_mtod_offset(struct rte_mbuf *mbuf, int offset) {
144         struct rte_mbuf *m;
145
146         for (m = mbuf; (m != NULL) && (offset > m->data_len); m = m->next)
147                 offset -= m->data_len;
148
149         if (m == NULL) {
150                 printf("pktmbuf_mtod_offset: offset out of buffer\n");
151                 return NULL;
152         }
153         return rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
154 }
155
156 static inline rte_iova_t
157 pktmbuf_iova_offset(struct rte_mbuf *mbuf, int offset) {
158         struct rte_mbuf *m;
159
160         for (m = mbuf; (m != NULL) && (offset > m->data_len); m = m->next)
161                 offset -= m->data_len;
162
163         if (m == NULL) {
164                 printf("pktmbuf_iova_offset: offset out of buffer\n");
165                 return 0;
166         }
167         return rte_pktmbuf_iova_offset(m, offset);
168 }
169
170 static inline struct rte_mbuf *
171 create_segmented_mbuf(struct rte_mempool *mbuf_pool, int pkt_len,
172                 int nb_segs, uint8_t pattern) {
173
174         struct rte_mbuf *m = NULL, *mbuf = NULL;
175         uint8_t *dst;
176         int data_len = 0;
177         int i, size;
178         int t_len;
179
180         if (pkt_len < 1) {
181                 printf("Packet size must be 1 or more (is %d)\n", pkt_len);
182                 return NULL;
183         }
184
185         if (nb_segs < 1) {
186                 printf("Number of segments must be 1 or more (is %d)\n",
187                                 nb_segs);
188                 return NULL;
189         }
190
191         t_len = pkt_len >= nb_segs ? pkt_len / nb_segs : 1;
192         size = pkt_len;
193
194         /* Create chained mbuf_src and fill it generated data */
195         for (i = 0; size > 0; i++) {
196
197                 m = rte_pktmbuf_alloc(mbuf_pool);
198                 if (i == 0)
199                         mbuf = m;
200
201                 if (m == NULL) {
202                         printf("Cannot create segment for source mbuf");
203                         goto fail;
204                 }
205
206                 /* Make sure if tailroom is zeroed */
207                 memset(m->buf_addr, pattern, m->buf_len);
208
209                 data_len = size > t_len ? t_len : size;
210                 dst = (uint8_t *)rte_pktmbuf_append(m, data_len);
211                 if (dst == NULL) {
212                         printf("Cannot append %d bytes to the mbuf\n",
213                                         data_len);
214                         goto fail;
215                 }
216
217                 if (mbuf != m)
218                         rte_pktmbuf_chain(mbuf, m);
219
220                 size -= data_len;
221
222         }
223         return mbuf;
224
225 fail:
226         if (mbuf)
227                 rte_pktmbuf_free(mbuf);
228         return NULL;
229 }
230
231 #endif /* TEST_CRYPTODEV_H_ */