NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
decompress_lz4_stream_sample.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 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_LZ4_STREAM);
42 
43 /*
44  * Run decompress_lz4_stream 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_lz4_stream(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  uint32_t output_crc_checksum = 0;
60  uint32_t output_xxh_checksum = 0;
61  uint32_t expected_output_xxh_checksum = 0;
62  bool has_content_checksum = false;
63  char *dst_buffer;
64  size_t data_len, written_len;
65  FILE *out_file;
66  doca_error_t result, tmp_result;
67  uint64_t max_buf_size;
68 
69  out_file = fopen(cfg->output_path, "wr");
70  if (out_file == NULL) {
71  DOCA_LOG_ERR("Unable to open output file: %s", cfg->output_path);
72  return DOCA_ERROR_NO_MEMORY;
73  }
74 
75  /* Allocate resources */
77  result = allocate_compress_resources(cfg->pci_address, max_bufs, &resources);
78  if (result != DOCA_SUCCESS) {
79  DOCA_LOG_ERR("Failed to allocate compress resources: %s", doca_error_get_descr(result));
80  goto close_file;
81  }
82  state = resources.state;
84  &max_buf_size);
85  if (result != DOCA_SUCCESS) {
86  DOCA_LOG_ERR("Failed to query decompress max buf size: %s", doca_error_get_descr(result));
87  goto destroy_resources;
88  }
89  if (file_size > max_buf_size) {
90  DOCA_LOG_ERR("Invalid file size. Should be smaller then %lu", max_buf_size);
92  goto destroy_resources;
93  }
94  /* Start compress context */
95  result = doca_ctx_start(state->ctx);
96  if (result != DOCA_SUCCESS) {
97  DOCA_LOG_ERR("Failed to start context: %s", doca_error_get_descr(result));
98  goto destroy_resources;
99  }
100 
101  dst_buffer = calloc(1, max_buf_size);
102  if (dst_buffer == NULL) {
103  DOCA_LOG_ERR("Failed to allocate memory: %s", doca_error_get_descr(result));
105  goto destroy_resources;
106  }
107 
108  result = doca_mmap_set_memrange(state->dst_mmap, dst_buffer, max_buf_size);
109  if (result != DOCA_SUCCESS) {
110  DOCA_LOG_ERR("Failed to set mmap memory range: %s", doca_error_get_descr(result));
111  goto free_dst_buf;
112  }
113  result = doca_mmap_start(state->dst_mmap);
114  if (result != DOCA_SUCCESS) {
115  DOCA_LOG_ERR("Failed to start mmap: %s", doca_error_get_descr(result));
116  goto free_dst_buf;
117  }
118 
119  result = doca_mmap_set_memrange(state->src_mmap, file_data, file_size);
120  if (result != DOCA_SUCCESS) {
121  DOCA_LOG_ERR("Failed to set mmap memory range: %s", doca_error_get_descr(result));
122  goto free_dst_buf;
123  }
124 
125  result = doca_mmap_start(state->src_mmap);
126  if (result != DOCA_SUCCESS) {
127  DOCA_LOG_ERR("Failed to start mmap: %s", doca_error_get_descr(result));
128  goto free_dst_buf;
129  }
130 
131  /* Construct DOCA buffer for each address range */
132  result =
133  doca_buf_inventory_buf_get_by_data(state->buf_inv, state->src_mmap, file_data, file_size, &src_doca_buf);
134  if (result != DOCA_SUCCESS) {
135  DOCA_LOG_ERR("Unable to acquire DOCA buffer representing source buffer: %s",
137  goto free_dst_buf;
138  }
139 
140  /* Construct DOCA buffer for each address range */
142  state->dst_mmap,
143  dst_buffer,
144  max_buf_size,
145  &dst_doca_buf);
146  if (result != DOCA_SUCCESS) {
147  DOCA_LOG_ERR("Unable to acquire DOCA buffer representing destination buffer: %s",
149  goto destroy_src_buf;
150  }
151 
152  if (cfg->is_with_frame) {
153  result = parse_lz4_frame(src_doca_buf, cfg, &has_content_checksum, &expected_output_xxh_checksum);
154  if (result != DOCA_SUCCESS) {
155  DOCA_LOG_ERR("Unable to parse LZ4 frame for given file: %s", doca_error_get_descr(result));
156  goto destroy_src_buf;
157  }
158  }
159 
160  /* Submit decompress task */
162  cfg->has_block_checksum,
163  cfg->are_blocks_independent,
164  src_doca_buf,
165  dst_doca_buf,
166  &output_crc_checksum,
167  &output_xxh_checksum);
168  if (result != DOCA_SUCCESS) {
169  DOCA_LOG_ERR("Decompress task failed: %s", doca_error_get_descr(result));
170  goto destroy_dst_buf;
171  }
172 
173  if (has_content_checksum && (output_xxh_checksum != expected_output_xxh_checksum)) {
174  DOCA_LOG_ERR("Decompress task failed: output checksum %x, doesn't match expected=%x",
175  output_xxh_checksum,
176  expected_output_xxh_checksum);
178  goto destroy_dst_buf;
179  }
180 
181  /* Write the result to output file */
182  result = doca_buf_get_data_len(dst_doca_buf, &data_len);
183  if (result != DOCA_SUCCESS) {
184  DOCA_LOG_ERR("Unable to get DOCA buffer data length for destination buffer: %s",
186  goto destroy_src_buf;
187  }
188 
189  written_len = fwrite(dst_buffer, sizeof(uint8_t), data_len, out_file);
190  if (written_len != data_len) {
191  DOCA_LOG_ERR("Failed to write the DOCA buffer representing destination buffer into a file");
193  goto destroy_dst_buf;
194  }
195 
196  DOCA_LOG_INFO("File was decompressed successfully and saved in: %s", cfg->output_path);
197  if (cfg->output_checksum) {
198  DOCA_LOG_INFO("The CRC Checksum is %u", output_crc_checksum);
199  DOCA_LOG_INFO("The xxHash Checksum is %u", output_xxh_checksum);
200  }
201 
202 destroy_dst_buf:
203  tmp_result = doca_buf_dec_refcount(dst_doca_buf, NULL);
204  if (tmp_result != DOCA_SUCCESS) {
205  DOCA_LOG_ERR("Failed to decrease DOCA destination buffer reference count: %s",
206  doca_error_get_descr(tmp_result));
207  DOCA_ERROR_PROPAGATE(result, tmp_result);
208  }
209 destroy_src_buf:
210  tmp_result = doca_buf_dec_refcount(src_doca_buf, NULL);
211  if (tmp_result != DOCA_SUCCESS) {
212  DOCA_LOG_ERR("Failed to decrease DOCA source buffer reference count: %s",
213  doca_error_get_descr(tmp_result));
214  DOCA_ERROR_PROPAGATE(result, tmp_result);
215  }
216 free_dst_buf:
217  free(dst_buffer);
218 destroy_resources:
219  tmp_result = destroy_compress_resources(&resources);
220  if (tmp_result != DOCA_SUCCESS) {
221  DOCA_LOG_ERR("Failed to destroy compress resources: %s", doca_error_get_descr(tmp_result));
222  DOCA_ERROR_PROPAGATE(result, tmp_result);
223  }
224 close_file:
225  fclose(out_file);
226 
227  return result;
228 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
doca_error_t destroy_compress_resources(struct compress_resources *resources)
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)
doca_error_t allocate_compress_resources(const char *pci_addr, uint32_t max_bufs, struct compress_resources *resources)
@ COMPRESS_MODE_DECOMPRESS_LZ4_STREAM
DOCA_LOG_REGISTER(DECOMPRESS_LZ4_STREAM)
doca_error_t decompress_lz4_stream(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...
static doca_error_t doca_buf_inventory_buf_get_by_data(struct doca_buf_inventory *inventory, struct doca_mmap *mmap, void *data, size_t data_len, struct doca_buf **buf)
Allocate single element from buffer inventory and point it to the buffer defined by data & data_len a...
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_EXPERIMENTAL doca_error_t doca_compress_cap_task_decompress_lz4_stream_get_max_buf_size(const struct doca_devinfo *devinfo, uint64_t *max_buffer_size)
Get decompress LZ4 stream 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