1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Cavium Networks
5 #include <rte_bus_vdev.h>
6 #include <rte_common.h>
8 #include "zlib_pmd_private.h"
10 /** Compute next mbuf in the list, assign data buffer and length,
11 * returns 0 if mbuf is NULL
13 #define COMPUTE_BUF(mbuf, data, len) \
14 ((mbuf = mbuf->next) ? \
15 (data = rte_pktmbuf_mtod(mbuf, uint8_t *)), \
16 (len = rte_pktmbuf_data_len(mbuf)) : 0)
19 process_zlib_deflate(struct rte_comp_op *op, z_stream *strm)
21 int ret, flush, fin_flush;
22 struct rte_mbuf *mbuf_src = op->m_src;
23 struct rte_mbuf *mbuf_dst = op->m_dst;
25 switch (op->flush_flag) {
26 case RTE_COMP_FLUSH_FULL:
27 case RTE_COMP_FLUSH_FINAL:
31 op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
32 ZLIB_PMD_ERR("Invalid flush value\n");
36 if (unlikely(!strm)) {
37 op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
38 ZLIB_PMD_ERR("Invalid z_stream\n");
41 /* Update z_stream with the inputs provided by application */
42 strm->next_in = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
45 strm->avail_in = rte_pktmbuf_data_len(mbuf_src) - op->src.offset;
47 strm->next_out = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
50 strm->avail_out = rte_pktmbuf_data_len(mbuf_dst) - op->dst.offset;
52 /* Set flush value to NO_FLUSH unless it is last mbuf */
54 /* Initialize status to SUCCESS */
55 op->status = RTE_COMP_OP_STATUS_SUCCESS;
58 /* Set flush value to Z_FINISH for last block */
59 if ((op->src.length - strm->total_in) <= strm->avail_in) {
60 strm->avail_in = (op->src.length - strm->total_in);
64 ret = deflate(strm, flush);
65 if (unlikely(ret == Z_STREAM_ERROR)) {
66 /* error return, do not process further */
67 op->status = RTE_COMP_OP_STATUS_ERROR;
70 /* Break if Z_STREAM_END is encountered */
71 if (ret == Z_STREAM_END)
74 /* Keep looping until input mbuf is consumed.
75 * Exit if destination mbuf gets exhausted.
77 } while ((strm->avail_out == 0) &&
78 COMPUTE_BUF(mbuf_dst, strm->next_out, strm->avail_out));
80 if (!strm->avail_out) {
81 /* there is no space for compressed output */
82 op->status = RTE_COMP_OP_STATUS_OUT_OF_SPACE_TERMINATED;
86 /* Update source buffer to next mbuf
87 * Exit if input buffers are fully consumed
89 } while (COMPUTE_BUF(mbuf_src, strm->next_in, strm->avail_in));
94 case RTE_COMP_OP_STATUS_SUCCESS:
95 op->consumed += strm->total_in;
97 case RTE_COMP_OP_STATUS_OUT_OF_SPACE_TERMINATED:
98 op->produced += strm->total_out;
101 ZLIB_PMD_ERR("stats not updated for status:%d\n",
109 process_zlib_inflate(struct rte_comp_op *op, z_stream *strm)
112 struct rte_mbuf *mbuf_src = op->m_src;
113 struct rte_mbuf *mbuf_dst = op->m_dst;
115 if (unlikely(!strm)) {
116 op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
117 ZLIB_PMD_ERR("Invalid z_stream\n");
120 strm->next_in = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
123 strm->avail_in = rte_pktmbuf_data_len(mbuf_src) - op->src.offset;
125 strm->next_out = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
128 strm->avail_out = rte_pktmbuf_data_len(mbuf_dst) - op->dst.offset;
130 /** Ignoring flush value provided from application for decompression */
132 /* initialize status to SUCCESS */
133 op->status = RTE_COMP_OP_STATUS_SUCCESS;
137 ret = inflate(strm, flush);
149 op->status = RTE_COMP_OP_STATUS_ERROR;
152 /* no further computation needed if
153 * Z_STREAM_END is encountered
161 /* Keep looping until input mbuf is consumed.
162 * Exit if destination mbuf gets exhausted.
164 } while ((strm->avail_out == 0) &&
165 COMPUTE_BUF(mbuf_dst, strm->next_out, strm->avail_out));
167 if (!strm->avail_out) {
168 /* there is no more space for decompressed output */
169 op->status = RTE_COMP_OP_STATUS_OUT_OF_SPACE_TERMINATED;
172 /* Read next input buffer to be processed, exit if compressed
173 * blocks are fully read
175 } while (COMPUTE_BUF(mbuf_src, strm->next_in, strm->avail_in));
178 /* Update op stats */
179 switch (op->status) {
180 case RTE_COMP_OP_STATUS_SUCCESS:
181 op->consumed += strm->total_in;
183 case RTE_COMP_OP_STATUS_OUT_OF_SPACE_TERMINATED:
184 op->produced += strm->total_out;
187 ZLIB_PMD_ERR("stats not produced for status:%d\n",
194 /** Process comp operation for mbuf */
196 process_zlib_op(struct zlib_qp *qp, struct rte_comp_op *op)
198 struct zlib_stream *stream;
199 struct zlib_priv_xform *private_xform;
201 if ((op->op_type == RTE_COMP_OP_STATEFUL) ||
202 (op->src.offset > rte_pktmbuf_data_len(op->m_src)) ||
203 (op->dst.offset > rte_pktmbuf_data_len(op->m_dst))) {
204 op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
205 ZLIB_PMD_ERR("Invalid source or destination buffers or "
206 "invalid Operation requested\n");
208 private_xform = (struct zlib_priv_xform *)op->private_xform;
209 stream = &private_xform->stream;
210 stream->comp(op, &stream->strm);
212 /* whatever is out of op, put it into completion queue with
215 return rte_ring_enqueue(qp->processed_pkts, (void *)op);
218 /** Parse comp xform and set private xform/Stream parameters */
220 zlib_set_stream_parameters(const struct rte_comp_xform *xform,
221 struct zlib_stream *stream)
223 int strategy, level, wbits;
224 z_stream *strm = &stream->strm;
226 /* allocate deflate state */
227 strm->zalloc = Z_NULL;
228 strm->zfree = Z_NULL;
229 strm->opaque = Z_NULL;
231 switch (xform->type) {
232 case RTE_COMP_COMPRESS:
233 stream->comp = process_zlib_deflate;
234 stream->free = deflateEnd;
235 /** Compression window bits */
236 switch (xform->compress.algo) {
237 case RTE_COMP_ALGO_DEFLATE:
238 wbits = -(xform->compress.window_size);
241 ZLIB_PMD_ERR("Compression algorithm not supported\n");
244 /** Compression Level */
245 switch (xform->compress.level) {
246 case RTE_COMP_LEVEL_PMD_DEFAULT:
247 level = Z_DEFAULT_COMPRESSION;
249 case RTE_COMP_LEVEL_NONE:
250 level = Z_NO_COMPRESSION;
252 case RTE_COMP_LEVEL_MIN:
253 level = Z_BEST_SPEED;
255 case RTE_COMP_LEVEL_MAX:
256 level = Z_BEST_COMPRESSION;
259 level = xform->compress.level;
260 if (level < RTE_COMP_LEVEL_MIN ||
261 level > RTE_COMP_LEVEL_MAX) {
262 ZLIB_PMD_ERR("Compression level %d "
269 /** Compression strategy */
270 switch (xform->compress.deflate.huffman) {
271 case RTE_COMP_HUFFMAN_DEFAULT:
272 strategy = Z_DEFAULT_STRATEGY;
274 case RTE_COMP_HUFFMAN_FIXED:
277 case RTE_COMP_HUFFMAN_DYNAMIC:
278 strategy = Z_DEFAULT_STRATEGY;
281 ZLIB_PMD_ERR("Compression strategy not supported\n");
284 if (deflateInit2(strm, level,
286 DEF_MEM_LEVEL, strategy) != Z_OK) {
287 ZLIB_PMD_ERR("Deflate init failed\n");
292 case RTE_COMP_DECOMPRESS:
293 stream->comp = process_zlib_inflate;
294 stream->free = inflateEnd;
296 switch (xform->decompress.algo) {
297 case RTE_COMP_ALGO_DEFLATE:
298 wbits = -(xform->decompress.window_size);
301 ZLIB_PMD_ERR("Compression algorithm not supported\n");
305 if (inflateInit2(strm, wbits) != Z_OK) {
306 ZLIB_PMD_ERR("Inflate init failed\n");
317 zlib_pmd_enqueue_burst(void *queue_pair,
318 struct rte_comp_op **ops, uint16_t nb_ops)
320 struct zlib_qp *qp = queue_pair;
324 for (i = 0; i < nb_ops; i++) {
325 ret = process_zlib_op(qp, ops[i]);
326 if (unlikely(ret < 0)) {
327 /* increment count if failed to push to completion
330 qp->qp_stats.enqueue_err_count++;
332 qp->qp_stats.enqueued_count++;
340 zlib_pmd_dequeue_burst(void *queue_pair,
341 struct rte_comp_op **ops, uint16_t nb_ops)
343 struct zlib_qp *qp = queue_pair;
345 unsigned int nb_dequeued = 0;
347 nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts,
348 (void **)ops, nb_ops, NULL);
349 qp->qp_stats.dequeued_count += nb_dequeued;
355 zlib_create(const char *name,
356 struct rte_vdev_device *vdev,
357 struct rte_compressdev_pmd_init_params *init_params)
359 struct rte_compressdev *dev;
361 dev = rte_compressdev_pmd_create(name, &vdev->device,
362 sizeof(struct zlib_private), init_params);
364 ZLIB_PMD_ERR("driver %s: create failed", init_params->name);
368 dev->dev_ops = rte_zlib_pmd_ops;
370 /* register rx/tx burst functions for data path */
371 dev->dequeue_burst = zlib_pmd_dequeue_burst;
372 dev->enqueue_burst = zlib_pmd_enqueue_burst;
378 zlib_probe(struct rte_vdev_device *vdev)
380 struct rte_compressdev_pmd_init_params init_params = {
385 const char *input_args;
388 name = rte_vdev_device_name(vdev);
393 input_args = rte_vdev_device_args(vdev);
395 retval = rte_compressdev_pmd_parse_input_args(&init_params, input_args);
398 "Failed to parse initialisation arguments[%s]\n",
403 return zlib_create(name, vdev, &init_params);
407 zlib_remove(struct rte_vdev_device *vdev)
409 struct rte_compressdev *compressdev;
412 name = rte_vdev_device_name(vdev);
416 compressdev = rte_compressdev_pmd_get_named_dev(name);
417 if (compressdev == NULL)
420 return rte_compressdev_pmd_destroy(compressdev);
423 static struct rte_vdev_driver zlib_pmd_drv = {
425 .remove = zlib_remove
428 RTE_PMD_REGISTER_VDEV(COMPRESSDEV_NAME_ZLIB_PMD, zlib_pmd_drv);
429 RTE_LOG_REGISTER(zlib_logtype_driver, pmd.compress.zlib, INFO);