NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
decompress_deflate_sample.c
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 #include <string.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <unistd.h>
30 
31 #include <doca_buf.h>
32 #include <doca_buf_inventory.h>
33 #include <doca_ctx.h>
34 #include <doca_compress.h>
35 #include <doca_error.h>
36 #include <doca_log.h>
37 
38 #include "common.h"
39 #include "compress_common.h"
40 
41 DOCA_LOG_REGISTER(DECOMPRESS_DEFLATE);
42 
43 /*
44  * Run decompress_deflate sample
45  *
46  * @cfg [in]: Configuration parameters
47  * @file_data [in]: file data for the decompress task
48  * @file_size [in]: file size
49  * @return: DOCA_SUCCESS on success, DOCA_ERROR otherwise.
50  */
51 doca_error_t decompress_deflate(struct compress_cfg *cfg, char *file_data, size_t file_size)
52 {
53  struct compress_resources resources = {0};
54  struct program_core_objects *state;
55  struct doca_buf *src_doca_buf;
56  struct doca_buf *dst_doca_buf;
57  /* The sample will use 2 doca buffers */
58  uint32_t max_bufs = 2;
59  uint64_t output_checksum = 0;
60  char *dst_buffer;
61  size_t data_len, written_len;
62  FILE *out_file;
63  doca_error_t result, tmp_result;
64  uint32_t source_adler_checksum, computed_adler_checksum;
65  uint64_t max_buf_size;
66 
67  bool zlib_compatible = cfg->is_with_frame;
68 
69  /* In case of compatibility with Zlib - verify the received file is valid */
70  if (zlib_compatible) {
71  if (file_size < ZLIB_COMPATIBILITY_ADDITIONAL_MEMORY) {
72  DOCA_LOG_ERR("Input file length is too short, must be at least %d bytes",
75  }
76 
78  if (result != DOCA_SUCCESS) {
79  DOCA_LOG_ERR("Failed to verify Zlib header: %s", doca_error_get_descr(result));
80  return result;
81  }
82  }
83 
84  out_file = fopen(cfg->output_path, "wr");
85  if (out_file == NULL) {
86  DOCA_LOG_ERR("Unable to open output file: %s", cfg->output_path);
87  return DOCA_ERROR_NO_MEMORY;
88  }
89 
90  /* Allocate resources */
92  result = allocate_compress_resources(cfg->pci_address, max_bufs, &resources);
93  if (result != DOCA_SUCCESS) {
94  DOCA_LOG_ERR("Failed to allocate compress resources: %s", doca_error_get_descr(result));
95  goto close_file;
96  }
97  state = resources.state;
99  &max_buf_size);
100  if (result != DOCA_SUCCESS) {
101  DOCA_LOG_ERR("Failed to query decompress max buf size: %s", doca_error_get_descr(result));
102  goto destroy_resources;
103  }
104  if (file_size > max_buf_size) {
105  DOCA_LOG_ERR("Invalid file size. Should be smaller then %lu", max_buf_size);
107  goto destroy_resources;
108  }
109  /* Start compress context */
110  result = doca_ctx_start(state->ctx);
111  if (result != DOCA_SUCCESS) {
112  DOCA_LOG_ERR("Failed to start context: %s", doca_error_get_descr(result));
113  goto destroy_resources;
114  }
115 
116  dst_buffer = calloc(1, max_buf_size);
117  if (dst_buffer == NULL) {
118  DOCA_LOG_ERR("Failed to allocate memory: %s", doca_error_get_descr(result));
120  goto destroy_resources;
121  }
122 
123  result = doca_mmap_set_memrange(state->dst_mmap, dst_buffer, max_buf_size);
124  if (result != DOCA_SUCCESS) {
125  DOCA_LOG_ERR("Failed to set mmap memory range: %s", doca_error_get_descr(result));
126  goto free_dst_buf;
127  }
128  result = doca_mmap_start(state->dst_mmap);
129  if (result != DOCA_SUCCESS) {
130  DOCA_LOG_ERR("Failed to start mmap: %s", doca_error_get_descr(result));
131  goto free_dst_buf;
132  }
133 
134  result = doca_mmap_set_memrange(state->src_mmap, file_data, file_size);
135  if (result != DOCA_SUCCESS) {
136  DOCA_LOG_ERR("Failed to set mmap memory range: %s", doca_error_get_descr(result));
137  goto free_dst_buf;
138  }
139 
140  result = doca_mmap_start(state->src_mmap);
141  if (result != DOCA_SUCCESS) {
142  DOCA_LOG_ERR("Failed to start mmap: %s", doca_error_get_descr(result));
143  goto free_dst_buf;
144  }
145 
146  /* Construct DOCA buffer for each address range */
147  result =
148  doca_buf_inventory_buf_get_by_addr(state->buf_inv, state->src_mmap, file_data, file_size, &src_doca_buf);
149  if (result != DOCA_SUCCESS) {
150  DOCA_LOG_ERR("Unable to acquire DOCA buffer representing source buffer: %s",
152  goto free_dst_buf;
153  }
154 
155  /* Construct DOCA buffer for each address range */
157  state->dst_mmap,
158  dst_buffer,
159  max_buf_size,
160  &dst_doca_buf);
161  if (result != DOCA_SUCCESS) {
162  DOCA_LOG_ERR("Unable to acquire DOCA buffer representing destination buffer: %s",
164  goto destroy_src_buf;
165  }
166 
167  /* Set data length in doca buffer */
168  if (zlib_compatible) {
169  /* Consider the Zlib header and the added checksum at the end */
170  result = doca_buf_set_data(src_doca_buf,
171  (void *)((uint8_t *)file_data + ZLIB_HEADER_SIZE),
173  if (result != DOCA_SUCCESS) {
174  DOCA_LOG_ERR("Unable to get data length in the DOCA buffer representing source buffer: %s",
176  goto destroy_dst_buf;
177  }
178  } else {
179  result = doca_buf_set_data(src_doca_buf, file_data, file_size);
180  if (result != DOCA_SUCCESS) {
181  DOCA_LOG_ERR("Unable to get data length in the DOCA buffer representing source buffer: %s",
183  goto destroy_dst_buf;
184  }
185  }
186 
187  /* Submit decompress task with checksum according to user configuration */
188  if (cfg->output_checksum || zlib_compatible) {
189  result = submit_decompress_deflate_task(&resources, src_doca_buf, dst_doca_buf, &output_checksum);
190  if (result != DOCA_SUCCESS) {
191  DOCA_LOG_ERR("Decompress task failed: %s", doca_error_get_descr(result));
192  goto destroy_dst_buf;
193  }
194  } else {
195  result = submit_decompress_deflate_task(&resources, src_doca_buf, dst_doca_buf, NULL);
196  if (result != DOCA_SUCCESS) {
197  DOCA_LOG_ERR("Decompress task failed: %s", doca_error_get_descr(result));
198  goto destroy_dst_buf;
199  }
200  }
201 
202  /* Verify that the computed Adler checksum matches the given adler checksum */
203  if (zlib_compatible) {
204  source_adler_checksum = be32toh(*(uint32_t *)((uint8_t *)file_data + file_size - ZLIB_TRAILER_SIZE));
205  computed_adler_checksum = (uint32_t)(output_checksum >> ADLER_CHECKSUM_SHIFT);
206 
207  if (source_adler_checksum != computed_adler_checksum) {
208  DOCA_LOG_ERR(
209  "The given Adler checksum=%u, doesn't match the computed Adler checksum=%u. Data may be corrupt",
210  source_adler_checksum,
211  computed_adler_checksum);
213  goto destroy_dst_buf;
214  }
215  }
216 
217  /* Write the result to output file */
218  result = doca_buf_get_data_len(dst_doca_buf, &data_len);
219  if (result != DOCA_SUCCESS) {
220  DOCA_LOG_ERR("Unable to get DOCA buffer data length for destination buffer: %s",
222  goto destroy_src_buf;
223  }
224 
225  written_len = fwrite(dst_buffer, sizeof(uint8_t), data_len, out_file);
226  if (written_len != data_len) {
227  DOCA_LOG_ERR("Failed to write the DOCA buffer representing destination buffer into a file");
229  goto destroy_dst_buf;
230  }
231 
232  DOCA_LOG_INFO("File was decompressed successfully and saved in: %s", cfg->output_path);
233  if (cfg->output_checksum)
234  DOCA_LOG_INFO("Checksum is %lu", output_checksum);
235 
236 destroy_dst_buf:
237  tmp_result = doca_buf_dec_refcount(dst_doca_buf, NULL);
238  if (tmp_result != DOCA_SUCCESS) {
239  DOCA_LOG_ERR("Failed to decrease DOCA destination buffer reference count: %s",
240  doca_error_get_descr(tmp_result));
241  DOCA_ERROR_PROPAGATE(result, tmp_result);
242  }
243 destroy_src_buf:
244  tmp_result = doca_buf_dec_refcount(src_doca_buf, NULL);
245  if (tmp_result != DOCA_SUCCESS) {
246  DOCA_LOG_ERR("Failed to decrease DOCA source buffer reference count: %s",
247  doca_error_get_descr(tmp_result));
248  DOCA_ERROR_PROPAGATE(result, tmp_result);
249  }
250 free_dst_buf:
251  free(dst_buffer);
252 destroy_resources:
253  tmp_result = destroy_compress_resources(&resources);
254  if (tmp_result != DOCA_SUCCESS) {
255  DOCA_LOG_ERR("Failed to destroy compress resources: %s", doca_error_get_descr(tmp_result));
256  DOCA_ERROR_PROPAGATE(result, tmp_result);
257  }
258 close_file:
259  fclose(out_file);
260 
261  return result;
262 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
doca_error_t destroy_compress_resources(struct compress_resources *resources)
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)
doca_error_t verify_compress_zlib_header(struct compress_zlib_header *zlib_header)
@ COMPRESS_MODE_DECOMPRESS_DEFLATE
#define ZLIB_TRAILER_SIZE
#define ZLIB_COMPATIBILITY_ADDITIONAL_MEMORY
#define ADLER_CHECKSUM_SHIFT
#define ZLIB_HEADER_SIZE
DOCA_LOG_REGISTER(DECOMPRESS_DEFLATE)
doca_error_t decompress_deflate(struct compress_cfg *cfg, char *file_data, size_t file_size)
struct rdma_resources resources
static doca_error_t doca_buf_inventory_buf_get_by_addr(struct doca_buf_inventory *inventory, struct doca_mmap *mmap, void *addr, size_t len, struct doca_buf **buf)
Allocate single element from buffer inventory and point it to the buffer defined by addr & len argume...
DOCA_STABLE doca_error_t doca_buf_dec_refcount(struct doca_buf *buf, uint16_t *refcount)
Decrease the object reference count by 1, if 0 reached, return the element back to the inventory.
DOCA_STABLE doca_error_t doca_buf_get_data_len(const struct doca_buf *buf, size_t *data_len)
Get buffer's data length.
DOCA_STABLE doca_error_t doca_buf_set_data(struct doca_buf *buf, void *data, size_t data_len)
DOCA_EXPERIMENTAL doca_error_t doca_compress_cap_task_decompress_deflate_get_max_buf_size(const struct doca_devinfo *devinfo, uint64_t *max_buffer_size)
Get decompress deflate max size.
DOCA_STABLE doca_error_t doca_ctx_start(struct doca_ctx *ctx)
Finalizes all configurations, and starts the DOCA CTX.
DOCA_STABLE struct doca_devinfo * doca_dev_as_devinfo(const struct doca_dev *dev)
Get local device info from device. This should be useful when wanting to query information about devi...
#define DOCA_ERROR_PROPAGATE(r, t)
Save the first encountered doca_error_t.
Definition: doca_error.h:83
enum doca_error doca_error_t
DOCA API return codes.
DOCA_STABLE const char * doca_error_get_descr(doca_error_t error)
Returns the description string of an error code.
@ DOCA_ERROR_INVALID_VALUE
Definition: doca_error.h:44
@ DOCA_ERROR_UNEXPECTED
Definition: doca_error.h:60
@ DOCA_ERROR_OPERATING_SYSTEM
Definition: doca_error.h:58
@ DOCA_SUCCESS
Definition: doca_error.h:38
@ DOCA_ERROR_NO_MEMORY
Definition: doca_error.h:45
#define DOCA_LOG_ERR(format,...)
Generates an ERROR application log message.
Definition: doca_log.h:466
#define DOCA_LOG_INFO(format,...)
Generates an INFO application log message.
Definition: doca_log.h:486
DOCA_STABLE doca_error_t doca_mmap_set_memrange(struct doca_mmap *mmap, void *addr, size_t len)
Set the memory range of DOCA memory map.
DOCA_STABLE doca_error_t doca_mmap_start(struct doca_mmap *mmap)
Start DOCA Memory Map.
const struct ip_frag_config * cfg
Definition: ip_frag_dp.c:0
struct doca_mmap * src_mmap
Definition: common.h:47
struct doca_buf_inventory * buf_inv
Definition: common.h:49
struct doca_dev * dev
Definition: common.h:46
struct doca_mmap * dst_mmap
Definition: common.h:48
struct doca_ctx * ctx
Definition: common.h:50