compressdev: add operation management
[dpdk.git] / lib / librte_compressdev / rte_comp.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017-2018 Intel Corporation
3  */
4
5 #ifndef _RTE_COMP_H_
6 #define _RTE_COMP_H_
7
8 /**
9  * @file rte_comp.h
10  *
11  * RTE definitions for Data Compression Service
12  *
13  */
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 #include <rte_mempool.h>
20 #include <rte_mbuf.h>
21
22 /** Status of comp operation */
23 enum rte_comp_op_status {
24         RTE_COMP_OP_STATUS_SUCCESS = 0,
25         /**< Operation completed successfully */
26         RTE_COMP_OP_STATUS_NOT_PROCESSED,
27         /**< Operation has not yet been processed by the device */
28         RTE_COMP_OP_STATUS_INVALID_ARGS,
29         /**< Operation failed due to invalid arguments in request */
30         RTE_COMP_OP_STATUS_ERROR,
31         /**< Error handling operation */
32         RTE_COMP_OP_STATUS_INVALID_STATE,
33         /**< Operation is invoked in invalid state */
34         RTE_COMP_OP_STATUS_OUT_OF_SPACE_TERMINATED,
35         /**< Output buffer ran out of space before operation completed.
36          * Error case. Application must resubmit all data with a larger
37          * output buffer.
38          */
39         RTE_COMP_OP_STATUS_OUT_OF_SPACE_RECOVERABLE,
40         /**< Output buffer ran out of space before operation completed, but this
41          * is not an error case. Output data up to op.produced can be used and
42          * next op in the stream should continue on from op.consumed+1.
43          */
44 };
45
46 /** Compression Algorithms */
47 enum rte_comp_algorithm {
48         RTE_COMP_ALGO_UNSPECIFIED = 0,
49         /** No Compression algorithm */
50         RTE_COMP_ALGO_NULL,
51         /**< No compression.
52          * Pass-through, data is copied unchanged from source buffer to
53          * destination buffer.
54          */
55         RTE_COMP_ALGO_DEFLATE,
56         /**< DEFLATE compression algorithm
57          * https://tools.ietf.org/html/rfc1951
58          */
59         RTE_COMP_ALGO_LZS,
60         /**< LZS compression algorithm
61          * https://tools.ietf.org/html/rfc2395
62          */
63         RTE_COMP_ALGO_LIST_END
64 };
65
66 /**< Compression Level.
67  * The number is interpreted by each PMD differently. However, lower numbers
68  * give fastest compression, at the expense of compression ratio while
69  * higher numbers may give better compression ratios but are likely slower.
70  */
71 #define RTE_COMP_LEVEL_PMD_DEFAULT      (-1)
72 /** Use PMD Default */
73 #define RTE_COMP_LEVEL_NONE             (0)
74 /** Output uncompressed blocks if supported by the specified algorithm */
75 #define RTE_COMP_LEVEL_MIN              (1)
76 /** Use minimum compression level supported by the PMD */
77 #define RTE_COMP_LEVEL_MAX              (9)
78 /** Use maximum compression level supported by the PMD */
79
80 /** Compression checksum types */
81 enum rte_comp_checksum_type {
82         RTE_COMP_CHECKSUM_NONE,
83         /**< No checksum generated */
84         RTE_COMP_CHECKSUM_CRC32,
85         /**< Generates a CRC32 checksum, as used by gzip */
86         RTE_COMP_CHECKSUM_ADLER32,
87         /**< Generates an Adler-32 checksum, as used by zlib */
88         RTE_COMP_CHECKSUM_CRC32_ADLER32,
89         /**< Generates both Adler-32 and CRC32 checksums, concatenated.
90          * CRC32 is in the lower 32bits, Adler-32 in the upper 32 bits.
91          */
92 };
93
94
95 /** Compression Huffman Type - used by DEFLATE algorithm */
96 enum rte_comp_huffman {
97         RTE_COMP_HUFFMAN_DEFAULT,
98         /**< PMD may choose which Huffman codes to use */
99         RTE_COMP_HUFFMAN_FIXED,
100         /**< Use Fixed Huffman codes */
101         RTE_COMP_HUFFMAN_DYNAMIC,
102         /**< Use Dynamic Huffman codes */
103 };
104
105 /** Compression flush flags */
106 enum rte_comp_flush_flag {
107         RTE_COMP_FLUSH_NONE,
108         /**< Data is not flushed. Output may remain in the compressor and be
109          * processed during a following op. It may not be possible to decompress
110          * output until a later op with some other flush flag has been sent.
111          */
112         RTE_COMP_FLUSH_SYNC,
113         /**< All data should be flushed to output buffer. Output data can be
114          * decompressed. However state and history is not cleared, so future
115          * operations may use history from this operation.
116          */
117         RTE_COMP_FLUSH_FULL,
118         /**< All data should be flushed to output buffer. Output data can be
119          * decompressed. State and history data is cleared, so future
120          * ops will be independent of ops processed before this.
121          */
122         RTE_COMP_FLUSH_FINAL
123         /**< Same as RTE_COMP_FLUSH_FULL but if op.algo is RTE_COMP_ALGO_DEFLATE
124          * then bfinal bit is set in the last block.
125          */
126 };
127
128 /** Compression transform types */
129 enum rte_comp_xform_type {
130         RTE_COMP_COMPRESS,
131         /**< Compression service - compress */
132         RTE_COMP_DECOMPRESS,
133         /**< Compression service - decompress */
134 };
135
136 /** Compression operation type */
137 enum rte_comp_op_type {
138         RTE_COMP_OP_STATELESS,
139         /**< All data to be processed is submitted in the op, no state or
140          * history from previous ops is used and none will be stored for future
141          * ops. Flush flag must be set to either FLUSH_FULL or FLUSH_FINAL.
142          */
143         RTE_COMP_OP_STATEFUL
144         /**< There may be more data to be processed after this op, it's part of
145          * a stream of data. State and history from previous ops can be used
146          * and resulting state and history can be stored for future ops,
147          * depending on flush flag.
148          */
149 };
150
151
152 /** Parameters specific to the deflate algorithm */
153 struct rte_comp_deflate_params {
154         enum rte_comp_huffman huffman;
155         /**< Compression huffman encoding type */
156 };
157
158 /** Setup Data for compression */
159 struct rte_comp_compress_xform {
160         enum rte_comp_algorithm algo;
161         /**< Algorithm to use for compress operation */
162         union {
163                 struct rte_comp_deflate_params deflate;
164                 /**< Parameters specific to the deflate algorithm */
165         }; /**< Algorithm specific parameters */
166         int level;
167         /**< Compression level */
168         uint8_t window_size;
169         /**< Base two log value of sliding window to be used. If window size
170          * can't be supported by the PMD then it may fall back to a smaller
171          * size. This is likely to result in a worse compression ratio.
172          */
173         enum rte_comp_checksum_type chksum;
174         /**< Type of checksum to generate on the uncompressed data */
175 };
176
177 /**
178  * Setup Data for decompression.
179  */
180 struct rte_comp_decompress_xform {
181         enum rte_comp_algorithm algo;
182         /**< Algorithm to use for decompression */
183         enum rte_comp_checksum_type chksum;
184         /**< Type of checksum to generate on the decompressed data */
185         uint8_t window_size;
186         /**< Base two log value of sliding window which was used to generate
187          * compressed data. If window size can't be supported by the PMD then
188          * setup of stream or private_xform should fail.
189          */
190 };
191
192 /**
193  * Compression transform structure.
194  *
195  * This is used to specify the compression transforms required.
196  * Each transform structure can hold a single transform, the type field is
197  * used to specify which transform is contained within the union.
198  */
199 struct rte_comp_xform {
200         enum rte_comp_xform_type type;
201         /**< xform type */
202         union {
203                 struct rte_comp_compress_xform compress;
204                 /**< xform for compress operation */
205                 struct rte_comp_decompress_xform decompress;
206                 /**< decompress xform */
207         };
208 };
209
210 /**
211  * Compression Operation.
212  *
213  * This structure contains data relating to performing a compression
214  * operation on the referenced mbuf data buffers.
215  *
216  * Comp operations are enqueued and dequeued in comp PMDs using the
217  * rte_compressdev_enqueue_burst() / rte_compressdev_dequeue_burst() APIs
218  */
219 struct rte_comp_op {
220         enum rte_comp_op_type op_type;
221         union {
222                 void *private_xform;
223                 /**< Stateless private PMD data derived from an rte_comp_xform.
224                  * A handle returned by rte_compressdev_private_xform_create()
225                  * must be attached to operations of op_type RTE_COMP_STATELESS.
226                  */
227                 void *stream;
228                 /**< Private PMD data derived initially from an rte_comp_xform,
229                  * which holds state and history data and evolves as operations
230                  * are processed. rte_compressdev_stream_create() must be called
231                  * on a device for all STATEFUL data streams and the resulting
232                  * stream attached to the one or more operations associated
233                  * with the data stream.
234                  * All operations in a stream must be sent to the same device.
235                  */
236         };
237
238         struct rte_mempool *mempool;
239         /**< Pool from which operation is allocated */
240         rte_iova_t iova_addr;
241         /**< IOVA address of this operation */
242         struct rte_mbuf *m_src;
243         /**< source mbuf
244          * The total size of the input buffer(s) can be retrieved using
245          * rte_pktmbuf_data_len(m_src)
246          */
247         struct rte_mbuf *m_dst;
248         /**< destination mbuf
249          * The total size of the output buffer(s) can be retrieved using
250          * rte_pktmbuf_data_len(m_dst)
251          */
252
253         struct {
254                 uint32_t offset;
255                 /**< Starting point for compression or decompression,
256                  * specified as number of bytes from start of packet in
257                  * source buffer.
258                  * Starting point for checksum generation in compress direction.
259                  */
260                 uint32_t length;
261                 /**< The length, in bytes, of the data in source buffer
262                  * to be compressed or decompressed.
263                  * Also the length of the data over which the checksum
264                  * should be generated in compress direction
265                  */
266         } src;
267         struct {
268                 uint32_t offset;
269                 /**< Starting point for writing output data, specified as
270                  * number of bytes from start of packet in dest
271                  * buffer. Starting point for checksum generation in
272                  * decompress direction.
273                  */
274         } dst;
275         enum rte_comp_flush_flag flush_flag;
276         /**< Defines flush characteristics for the output data.
277          * Only applicable in compress direction
278          */
279         uint64_t input_chksum;
280         /**< An input checksum can be provided to generate a
281          * cumulative checksum across sequential blocks in a STATELESS stream.
282          * Checksum type is as specified in xform chksum_type
283          */
284         uint64_t output_chksum;
285         /**< If a checksum is generated it will be written in here.
286          * Checksum type is as specified in xform chksum_type.
287          */
288         uint32_t consumed;
289         /**< The number of bytes from the source buffer
290          * which were compressed/decompressed.
291          */
292         uint32_t produced;
293         /**< The number of bytes written to the destination buffer
294          * which were compressed/decompressed.
295          */
296         uint64_t debug_status;
297         /**<
298          * Status of the operation is returned in the status param.
299          * This field allows the PMD to pass back extra
300          * pmd-specific debug information. Value is not defined on the API.
301          */
302         uint8_t status;
303         /**<
304          * Operation status - use values from enum rte_comp_status.
305          * This is reset to
306          * RTE_COMP_OP_STATUS_NOT_PROCESSED on allocation from mempool and
307          * will be set to RTE_COMP_OP_STATUS_SUCCESS after operation
308          * is successfully processed by a PMD
309          */
310 } __rte_cache_aligned;
311
312 /**
313  * Creates an operation pool
314  *
315  * @param name
316  *   Compress pool name
317  * @param nb_elts
318  *   Number of elements in pool
319  * @param cache_size
320  *   Number of elements to cache on lcore, see
321  *   *rte_mempool_create* for further details about cache size
322  * @param user_size
323  *   Size of private data to allocate for user with each operation
324  * @param socket_id
325  *   Socket to identifier allocate memory on
326  * @return
327  *  - On success pointer to mempool
328  *  - On failure NULL
329  */
330 struct rte_mempool * __rte_experimental
331 rte_comp_op_pool_create(const char *name,
332                 unsigned int nb_elts, unsigned int cache_size,
333                 uint16_t user_size, int socket_id);
334
335 /**
336  * Allocate an operation from a mempool with default parameters set
337  *
338  * @param mempool
339  *   Compress operation mempool
340  *
341  * @return
342  * - On success returns a valid rte_comp_op structure
343  * - On failure returns NULL
344  */
345 struct rte_comp_op * __rte_experimental
346 rte_comp_op_alloc(struct rte_mempool *mempool);
347
348 /**
349  * Bulk allocate operations from a mempool with default parameters set
350  *
351  * @param mempool
352  *   Compress operation mempool
353  * @param ops
354  *   Array to place allocated operations
355  * @param nb_ops
356  *   Number of operations to allocate
357  * @return
358  *   - 0: Success
359  *   - -ENOENT: Not enough entries in the mempool; no ops are retrieved.
360  */
361 int __rte_experimental
362 rte_comp_op_bulk_alloc(struct rte_mempool *mempool,
363                 struct rte_comp_op **ops, uint16_t nb_ops);
364
365 /**
366  * Free operation structure
367  * If operation has been allocate from a rte_mempool, then the operation will
368  * be returned to the mempool.
369  *
370  * @param op
371  *   Compress operation
372  */
373 void __rte_experimental
374 rte_comp_op_free(struct rte_comp_op *op);
375
376 #ifdef __cplusplus
377 }
378 #endif
379
380 #endif /* _RTE_COMP_H_ */