NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
compress_common.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
26 #ifndef COMPRESS_COMMON_H_
27 #define COMPRESS_COMMON_H_
28 
29 #include <stdbool.h>
30 #include <stddef.h>
31 #include <stdint.h>
32 
33 #include <doca_dev.h>
34 #include <doca_compress.h>
35 #include <doca_mmap.h>
36 #include <doca_error.h>
37 
38 #define USER_MAX_FILE_NAME 255 /* Max file name length */
39 #define MAX_FILE_NAME (USER_MAX_FILE_NAME + 1) /* Max file name string length */
40 #define SLEEP_IN_NANOS (10 * 1000) /* Sample the task every 10 microseconds */
41 #define NUM_COMPRESS_TASKS (1) /* Number of compress tasks */
42 #define ADLER_CHECKSUM_SHIFT (32) /* The shift for the Adler checksum within the output checksum */
43 #define ZLIB_HEADER_SIZE (2) /* The header size in zlib */
44 #define ZLIB_TRAILER_SIZE (4) /* The trailer size in zlib (the 32-bit checksum) */
45 /* Additional memory for zlib compatibility, used in allocation and reading */
46 #define ZLIB_COMPATIBILITY_ADDITIONAL_MEMORY (ZLIB_HEADER_SIZE + ZLIB_TRAILER_SIZE)
47 
48 /* Compress modes */
50  COMPRESS_MODE_COMPRESS_DEFLATE, /* Compress mode */
51  COMPRESS_MODE_DECOMPRESS_DEFLATE, /* Decompress mode with deflate algorithm */
52  COMPRESS_MODE_DECOMPRESS_LZ4_STREAM, /* Decompress stream mode with lz4 algorithm */
53 };
54 
55 /* Configuration struct */
56 struct compress_cfg {
57  char file_path[MAX_FILE_NAME]; /* File to compress/decompress */
58  char output_path[MAX_FILE_NAME]; /* Output file */
59  char pci_address[DOCA_DEVINFO_PCI_ADDR_SIZE]; /* Device PCI address */
60  enum compress_mode mode; /* Compress task type */
61  bool is_with_frame; /* Write \ read a file with a frame.
62  * In deflate - compatible with default zlib settings.
63  * In LZ4 - compatible with LZ4 frame format */
64  bool has_block_checksum; /* For use only in LZ4 stream sample, flag to indicate if blocks
65  * have a checksum */
66  bool are_blocks_independent; /* For use only in LZ4 stream sample, flag to indicate if blocks
67  * are independent */
68  bool output_checksum; /* To output checksum or not */
69 };
70 
71 /* DOCA compress resources */
73  struct program_core_objects *state; /* DOCA program core objects */
74  struct doca_compress *compress; /* DOCA compress context */
75  size_t num_remaining_tasks; /* Number of remaining compress tasks */
76  enum compress_mode mode; /* Compress mode - compress/decompress */
77  bool run_pe_progress; /* Controls whether progress loop should run */
78 };
79 
80 /* A zlib header, for compatibility with third-party applications */
82  uint8_t cmf;
83  uint8_t flg;
84 } __attribute__((packed));
85 
86 /*
87  * Register the command line parameters for all compress samples
88  *
89  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
90  */
92 
93 /*
94  * Register the command line parameters for deflate samples
95  *
96  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
97  */
99 
100 /*
101  * Register the command line parameters for lz4 stream sample
102  *
103  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
104  */
106 
107 /*
108  * Initiate the fields of the zlib header with default values
109  *
110  * @zlib_header [in]: A Zlib header to initiate with DOCA Compress default settings
111  */
112 void init_compress_zlib_header(struct compress_zlib_header *zlib_header);
113 
114 /*
115  * Verify the header values are valid and compatible with DOCA compress
116  *
117  * @zlib_header [in]: A Zlib header to initiate with DOCA Compress default settings
118  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
119  */
121 
122 /*
123  * Parse the LZ4 frame and verify the header values are valid and compatible with DOCA compress
124  *
125  * @src_buf [in]: The src buffer, beginning with the frame header. Data section will be updated accordingly.
126  * @cfg [in/out]: The compress configuration, in order to update relevant flags
127  * @has_content_checksum [out]: true if the content includes a checksum (ignored if the pointer is NULL).
128  * @content_checksum [out]: A pointer to hold the content checksum, if present (ignored if the pointer is NULL).
129  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
130  */
131 doca_error_t parse_lz4_frame(struct doca_buf *src_buf,
132  struct compress_cfg *cfg,
133  bool *has_content_checksum,
134  uint32_t *content_checksum);
135 
136 /*
137  * Allocate DOCA compress resources
138  *
139  * @pci_addr [in]: Device PCI address
140  * @max_bufs [in]: Maximum number of buffers for DOCA Inventory
141  * @resources [out]: DOCA compress resources to allocate
142  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
143  */
144 doca_error_t allocate_compress_resources(const char *pci_addr, uint32_t max_bufs, struct compress_resources *resources);
145 
146 /*
147  * Destroy DOCA compress resources
148  *
149  * @resources [in]: DOCA compress resources to destroy
150  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
151  */
153 
154 /*
155  * Submit compress deflate task and wait for completion
156  * Also calculate the checksum where the lower 32 bits contain the CRC checksum result
157  * and the upper 32 bits contain the Adler checksum result.
158  *
159  * @resources [in]: DOCA compress resources
160  * @src_buf [in]: Source buffer
161  * @dst_buf [in]: Destination buffer
162  * @output_checksum [out]: The calculated checksum
163  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
164  */
166  struct doca_buf *src_buf,
167  struct doca_buf *dst_buf,
168  uint64_t *output_checksum);
169 
170 /*
171  * Submit decompress deflate task and wait for completion
172  * Also calculate the checksum where the lower 32 bits contain the CRC checksum result
173  * and the upper 32 bits contain the Adler checksum result.
174  *
175  * @resources [in]: DOCA compress resources
176  * @src_buf [in]: Source buffer
177  * @dst_buf [in]: Destination buffer
178  * @output_checksum [out]: The calculated checksum
179  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
180  */
182  struct doca_buf *src_buf,
183  struct doca_buf *dst_buf,
184  uint64_t *output_checksum);
185 
186 /*
187  * Submit decompress lz4 task and wait for completion
188  * Also calculate the checksum where the lower 32 bits contain the CRC checksum result
189  * and the upper 32 bits contain the Adler checksum result.
190  *
191  * @resources [in]: DOCA compress resources
192  * @has_block_checksum [in]: has_block_checksum flag
193  * @are_blocks_independent [in]: are_blocks_independent flag
194  * @src_buf [in]: Source buffer
195  * @dst_buf [in]: Destination buffer
196  * @output_crc_checksum [out]: The calculated crc checksum
197  * @output_xxh_checksum [out]: The calculated xxHash checksum
198  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
199  */
201  uint8_t has_block_checksum,
202  uint8_t are_blocks_independent,
203  struct doca_buf *src_buf,
204  struct doca_buf *dst_buf,
205  uint32_t *output_crc_checksum,
206  uint32_t *output_xxh_checksum);
207 
208 /*
209  * Check if given device is capable of executing a DOCA compress deflate task.
210  *
211  * @devinfo [in]: The DOCA device information
212  * @return: DOCA_SUCCESS if the device supports DOCA compress deflate task and DOCA_ERROR otherwise
213  */
214 doca_error_t compress_task_compress_deflate_is_supported(struct doca_devinfo *devinfo);
215 
216 /*
217  * Check if given device is capable of executing a DOCA decompress deflate task.
218  *
219  * @devinfo [in]: The DOCA device information
220  * @return: DOCA_SUCCESS if the device supports DOCA decompress deflate task and DOCA_ERROR otherwise
221  */
222 doca_error_t compress_task_decompress_deflate_is_supported(struct doca_devinfo *devinfo);
223 
224 /*
225  * Check if given device is capable of executing a DOCA decompress deflate task.
226  *
227  * @devinfo [in]: The DOCA device information
228  * @return: DOCA_SUCCESS if the device supports DOCA decompress deflate task and DOCA_ERROR otherwise
229  */
231 /*
232  * Compress task completed callback
233  *
234  * @compress_task [in]: Completed task
235  * @task_user_data [in]: doca_data from the task
236  * @ctx_user_data [in]: doca_data from the context
237  */
238 void compress_completed_callback(struct doca_compress_task_compress_deflate *compress_task,
239  union doca_data task_user_data,
240  union doca_data ctx_user_data);
241 
242 /*
243  * Compress task error callback
244  *
245  * @compress_task [in]: failed task
246  * @task_user_data [in]: doca_data from the task
247  * @ctx_user_data [in]: doca_data from the context
248  */
249 void compress_error_callback(struct doca_compress_task_compress_deflate *compress_task,
250  union doca_data task_user_data,
251  union doca_data ctx_user_data);
252 
253 /*
254  * Decompress deflate task completed callback
255  *
256  * @decompress_task [in]: Completed task
257  * @task_user_data [in]: doca_data from the task
258  * @ctx_user_data [in]: doca_data from the context
259  */
260 void decompress_deflate_completed_callback(struct doca_compress_task_decompress_deflate *decompress_task,
261  union doca_data task_user_data,
262  union doca_data ctx_user_data);
263 
264 /*
265  * Decompress deflate task error callback
266  *
267  * @decompress_task [in]: failed task
268  * @task_user_data [in]: doca_data from the task
269  * @ctx_user_data [in]: doca_data from the context
270  */
271 void decompress_deflate_error_callback(struct doca_compress_task_decompress_deflate *decompress_task,
272  union doca_data task_user_data,
273  union doca_data ctx_user_data);
274 
275 /*
276  * Decompress lz4 stream task completed callback
277  *
278  * @decompress_task [in]: Completed task
279  * @task_user_data [in]: doca_data from the task
280  * @ctx_user_data [in]: doca_data from the context
281  */
282 void decompress_lz4_stream_completed_callback(struct doca_compress_task_decompress_lz4_stream *decompress_task,
283  union doca_data task_user_data,
284  union doca_data ctx_user_data);
285 
286 /*
287  * Decompress lz4 stream task error callback
288  *
289  * @decompress_task [in]: failed task
290  * @task_user_data [in]: doca_data from the task
291  * @ctx_user_data [in]: doca_data from the context
292  */
293 void decompress_lz4_stream_error_callback(struct doca_compress_task_decompress_lz4_stream *decompress_task,
294  union doca_data task_user_data,
295  union doca_data ctx_user_data);
296 
297 #endif /* COMPRESS_COMMON_H_ */
doca_error_t register_compress_params(void)
doca_error_t register_lz4_stream_params(void)
compress_mode
@ COMPRESS_MODE_DECOMPRESS_LZ4_STREAM
@ COMPRESS_MODE_COMPRESS_DEFLATE
@ COMPRESS_MODE_DECOMPRESS_DEFLATE
doca_error_t register_deflate_params(void)
doca_error_t destroy_compress_resources(struct compress_resources *resources)
doca_error_t compress_task_compress_deflate_is_supported(struct doca_devinfo *devinfo)
void init_compress_zlib_header(struct compress_zlib_header *zlib_header)
void compress_error_callback(struct doca_compress_task_compress_deflate *compress_task, union doca_data task_user_data, union doca_data ctx_user_data)
void decompress_deflate_completed_callback(struct doca_compress_task_decompress_deflate *decompress_task, union doca_data task_user_data, union doca_data ctx_user_data)
doca_error_t compress_task_decompress_lz4_stream_is_supported(struct doca_devinfo *devinfo)
void decompress_deflate_error_callback(struct doca_compress_task_decompress_deflate *decompress_task, union doca_data task_user_data, union doca_data ctx_user_data)
doca_error_t parse_lz4_frame(struct doca_buf *src_buf, struct compress_cfg *cfg, bool *has_content_checksum, uint32_t *content_checksum)
doca_error_t submit_decompress_lz4_stream_task(struct compress_resources *resources, uint8_t has_block_checksum, uint8_t are_blocks_independent, struct doca_buf *src_buf, struct doca_buf *dst_buf, uint32_t *output_crc_checksum, uint32_t *output_xxh_checksum)
struct compress_zlib_header __attribute__((packed))
void compress_completed_callback(struct doca_compress_task_compress_deflate *compress_task, union doca_data task_user_data, union doca_data ctx_user_data)
void decompress_lz4_stream_completed_callback(struct doca_compress_task_decompress_lz4_stream *decompress_task, union doca_data task_user_data, union doca_data ctx_user_data)
doca_error_t compress_task_decompress_deflate_is_supported(struct doca_devinfo *devinfo)
void decompress_lz4_stream_error_callback(struct doca_compress_task_decompress_lz4_stream *decompress_task, union doca_data task_user_data, union doca_data ctx_user_data)
doca_error_t allocate_compress_resources(const char *pci_addr, uint32_t max_bufs, struct compress_resources *resources)
doca_error_t submit_decompress_deflate_task(struct compress_resources *resources, struct doca_buf *src_buf, struct doca_buf *dst_buf, uint64_t *output_checksum)
#define MAX_FILE_NAME
doca_error_t verify_compress_zlib_header(struct compress_zlib_header *zlib_header)
doca_error_t submit_compress_deflate_task(struct compress_resources *resources, struct doca_buf *src_buf, struct doca_buf *dst_buf, uint64_t *output_checksum)
struct rdma_resources resources
#define DOCA_DEVINFO_PCI_ADDR_SIZE
Buffer size to hold PCI BDF format: "XXXX:XX:XX.X". Including a null terminator.
Definition: doca_dev.h:313
enum doca_error doca_error_t
DOCA API return codes.
const struct ip_frag_config * cfg
Definition: ip_frag_dp.c:0
enum compress_mode mode
char file_path[MAX_FILE_NAME]
bool are_blocks_independent
bool has_block_checksum
char pci_address[DOCA_DEVINFO_PCI_ADDR_SIZE]
char output_path[MAX_FILE_NAME]
struct program_core_objects * state
struct doca_compress * compress
enum compress_mode mode
Convenience type for representing opaque data.
Definition: doca_types.h:56