NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
compress_deflate_sample.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022-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(COMPRESS_DEFLATE);
42 
43 /*
44  * Run compress_deflate sample
45  *
46  * @cfg [in]: Configuration parameters
47  * @file_data [in]: file data for the compress task
48  * @file_size [in]: file size
49  * @return: DOCA_SUCCESS on success, DOCA_ERROR otherwise.
50  */
51 doca_error_t compress_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  uint32_t adler_checksum = 0;
61  doca_be32_t be_adler_checksum;
62  char *dst_buffer;
63  void *dst_buf_data, *dst_buf_tail;
64  size_t data_len, write_len, written_len;
65  FILE *out_file;
66  doca_error_t result, tmp_result;
67  uint64_t max_buf_size, max_output_size;
68 
69  bool zlib_compatible = cfg->is_with_frame;
70 
71  out_file = fopen(cfg->output_path, "wr");
72  if (out_file == NULL) {
73  DOCA_LOG_ERR("Unable to open output file: %s", cfg->output_path);
74  return DOCA_ERROR_NO_MEMORY;
75  }
76 
77  /* Allocate resources */
79  result = allocate_compress_resources(cfg->pci_address, max_bufs, &resources);
80  if (result != DOCA_SUCCESS) {
81  DOCA_LOG_ERR("Failed to allocate compress resources: %s", doca_error_get_descr(result));
82  goto close_file;
83  }
84  state = resources.state;
85 
87  &max_buf_size);
88  if (result != DOCA_SUCCESS) {
89  DOCA_LOG_ERR("Failed to query compress max buf size: %s", doca_error_get_descr(result));
90  goto destroy_resources;
91  }
92  if (file_size > max_buf_size) {
93  DOCA_LOG_ERR("Invalid file size. Should be smaller than %lu", max_buf_size);
95  goto destroy_resources;
96  }
97 
98  max_output_size = max_buf_size;
99  /* Consider the Zlib header and the added checksum at the end */
100  if (zlib_compatible)
101  max_output_size += ZLIB_COMPATIBILITY_ADDITIONAL_MEMORY;
102 
103  /* Start compress context */
104  result = doca_ctx_start(state->ctx);
105  if (result != DOCA_SUCCESS) {
106  DOCA_LOG_ERR("Failed to start context: %s", doca_error_get_descr(result));
107  goto destroy_resources;
108  }
109 
110  dst_buffer = calloc(1, max_output_size);
111  if (dst_buffer == NULL) {
112  DOCA_LOG_ERR("Failed to allocate memory: %s", doca_error_get_descr(result));
114  goto destroy_resources;
115  }
116 
117  result = doca_mmap_set_memrange(state->dst_mmap, dst_buffer, max_output_size);
118  if (result != DOCA_SUCCESS) {
119  DOCA_LOG_ERR("Failed to set mmap memory range: %s", doca_error_get_descr(result));
120  goto free_dst_buf;
121  }
122  result = doca_mmap_start(state->dst_mmap);
123  if (result != DOCA_SUCCESS) {
124  DOCA_LOG_ERR("Failed to start mmap: %s", doca_error_get_descr(result));
125  goto free_dst_buf;
126  }
127 
128  result = doca_mmap_set_memrange(state->src_mmap, file_data, file_size);
129  if (result != DOCA_SUCCESS) {
130  DOCA_LOG_ERR("Failed to set mmap memory range: %s", doca_error_get_descr(result));
131  goto free_dst_buf;
132  }
133 
134  result = doca_mmap_start(state->src_mmap);
135  if (result != DOCA_SUCCESS) {
136  DOCA_LOG_ERR("Failed to start mmap: %s", doca_error_get_descr(result));
137  goto free_dst_buf;
138  }
139 
140  /* Construct DOCA buffer for each address range */
141  result =
142  doca_buf_inventory_buf_get_by_addr(state->buf_inv, state->src_mmap, file_data, file_size, &src_doca_buf);
143  if (result != DOCA_SUCCESS) {
144  DOCA_LOG_ERR("Unable to acquire DOCA buffer representing source buffer: %s",
146  goto free_dst_buf;
147  }
148 
149  /* Construct DOCA buffer for each address range */
151  state->dst_mmap,
152  dst_buffer,
153  max_buf_size,
154  &dst_doca_buf);
155  if (result != DOCA_SUCCESS) {
156  DOCA_LOG_ERR("Unable to acquire DOCA buffer representing destination buffer: %s",
158  goto destroy_src_buf;
159  }
160 
161  /* Set data length in doca buffer */
162  result = doca_buf_set_data(src_doca_buf, file_data, file_size);
163  if (result != DOCA_SUCCESS) {
164  DOCA_LOG_ERR("Unable to set data in the DOCA buffer representing source buffer: %s",
166  goto destroy_dst_buf;
167  }
168 
169  if (zlib_compatible) {
170  /* Set data pointer to reserve space for the header */
171  result = doca_buf_set_data(dst_doca_buf, dst_buffer + ZLIB_HEADER_SIZE, 0);
172  if (result != DOCA_SUCCESS) {
173  DOCA_LOG_ERR("Unable to set data in the DOCA buffer representing destination buffer: %s",
175  goto destroy_dst_buf;
176  }
177  }
178 
179  if (cfg->output_checksum || zlib_compatible) {
180  result = submit_compress_deflate_task(&resources, src_doca_buf, dst_doca_buf, &output_checksum);
181  if (result != DOCA_SUCCESS) {
182  DOCA_LOG_ERR("Compress task failed: %s", doca_error_get_descr(result));
183  goto destroy_dst_buf;
184  }
185  } else {
186  result = submit_compress_deflate_task(&resources, src_doca_buf, dst_doca_buf, NULL);
187  if (result != DOCA_SUCCESS) {
188  DOCA_LOG_ERR("Compress task failed: %s", doca_error_get_descr(result));
189  goto destroy_dst_buf;
190  }
191  }
192 
193  result = doca_buf_get_data_len(dst_doca_buf, &data_len);
194  if (result != DOCA_SUCCESS) {
195  DOCA_LOG_ERR("Unable to get data length in the DOCA buffer representing destination buffer: %s",
197  goto destroy_dst_buf;
198  }
199  write_len = data_len;
200 
201  if (zlib_compatible) {
202  /* Write the Zlib header in the reserved header space */
203  init_compress_zlib_header((struct compress_zlib_header *)dst_buffer);
204 
205  /* Retrieve the data address and compute the end of the data section */
206  result = doca_buf_get_data(dst_doca_buf, &dst_buf_data);
207  if (result != DOCA_SUCCESS) {
208  DOCA_LOG_ERR("Unable to get data length in the DOCA buffer representing destination buffer: %s",
210  goto destroy_dst_buf;
211  }
212  dst_buf_tail = ((uint8_t *)dst_buf_data + data_len);
213 
214  /* Set data pointer to consider the added checksum */
215  data_len += ZLIB_TRAILER_SIZE;
216  result = doca_buf_set_data(dst_doca_buf, dst_buf_data, data_len);
217  if (result != DOCA_SUCCESS) {
218  DOCA_LOG_ERR("Unable to set data in the DOCA buffer representing destination buffer: %s",
220  goto destroy_dst_buf;
221  }
222 
223  /* Extract the Adler32 checksum from the output_checksum and write it after the compressed data */
224  adler_checksum = (uint32_t)(output_checksum >> ADLER_CHECKSUM_SHIFT);
225  be_adler_checksum = htobe32(adler_checksum);
226 
227  memcpy(dst_buf_tail, &be_adler_checksum, ZLIB_TRAILER_SIZE);
228 
229  /* Consider the Zlib header and the added checksum at the end */
231  }
232 
233  /* Write the result to output file */
234  written_len = fwrite(dst_buffer, sizeof(uint8_t), write_len, out_file);
235  if (written_len != write_len) {
236  DOCA_LOG_ERR("Failed to write the DOCA buffer representing destination buffer into a file");
238  goto destroy_dst_buf;
239  }
240 
241  DOCA_LOG_INFO("File was compressed successfully and saved in: %s", cfg->output_path);
242  if (cfg->output_checksum)
243  DOCA_LOG_INFO("Checksum is %lu", output_checksum);
244 
245 destroy_dst_buf:
246  tmp_result = doca_buf_dec_refcount(dst_doca_buf, NULL);
247  if (tmp_result != DOCA_SUCCESS) {
248  DOCA_LOG_ERR("Failed to decrease DOCA destination buffer reference count: %s",
249  doca_error_get_descr(tmp_result));
250  DOCA_ERROR_PROPAGATE(result, tmp_result);
251  }
252 destroy_src_buf:
253  tmp_result = doca_buf_dec_refcount(src_doca_buf, NULL);
254  if (tmp_result != DOCA_SUCCESS) {
255  DOCA_LOG_ERR("Failed to decrease DOCA source buffer reference count: %s",
256  doca_error_get_descr(tmp_result));
257  DOCA_ERROR_PROPAGATE(result, tmp_result);
258  }
259 free_dst_buf:
260  free(dst_buffer);
261 destroy_resources:
262  tmp_result = destroy_compress_resources(&resources);
263  if (tmp_result != DOCA_SUCCESS) {
264  DOCA_LOG_ERR("Failed to destroy compress resources: %s", doca_error_get_descr(tmp_result));
265  DOCA_ERROR_PROPAGATE(result, tmp_result);
266  }
267 close_file:
268  fclose(out_file);
269 
270  return result;
271 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
doca_error_t destroy_compress_resources(struct compress_resources *resources)
void init_compress_zlib_header(struct compress_zlib_header *zlib_header)
doca_error_t allocate_compress_resources(const char *pci_addr, uint32_t max_bufs, struct compress_resources *resources)
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)
@ COMPRESS_MODE_COMPRESS_DEFLATE
#define ZLIB_TRAILER_SIZE
#define ZLIB_COMPATIBILITY_ADDITIONAL_MEMORY
#define ADLER_CHECKSUM_SHIFT
#define ZLIB_HEADER_SIZE
doca_error_t compress_deflate(struct compress_cfg *cfg, char *file_data, size_t file_size)
DOCA_LOG_REGISTER(COMPRESS_DEFLATE)
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(const struct doca_buf *buf, void **data)
Get the buffer's data.
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_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.
uint32_t doca_be32_t
Definition: doca_types.h:121
const struct ip_frag_config * cfg
Definition: ip_frag_dp.c:0
#define htobe32
Definition: os_utils.hpp:40
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