File: zlib_pmd.c

package info (click to toggle)
dpdk 25.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 127,892 kB
  • sloc: ansic: 2,358,479; python: 16,426; sh: 4,474; makefile: 1,713; awk: 70
file content (585 lines) | stat: -rw-r--r-- 15,328 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2018 Cavium Networks
 */

#include <bus_vdev_driver.h>
#include <rte_common.h>
#include <stdlib.h>

#include "zlib_pmd_private.h"

/** Compute next mbuf in the list, assign data buffer and length,
 *  returns 0 if mbuf is NULL
 */
#define COMPUTE_BUF(mbuf, data, len)		\
		((mbuf = mbuf->next) ?		\
		(data = rte_pktmbuf_mtod(mbuf, uint8_t *)),	\
		(len = rte_pktmbuf_data_len(mbuf)) : 0)

#define BOTTOM_NIBBLE_OF_BYTE 0xf
#define TOP_NIBBLE_OF_BYTE 0xf0
#define BOTTOM_NIBBLE_OF_BYTES_IN_DOUBLE_WORD 0x0f0f0f0f
#define TOP_NIBBLE_OF_BYTE_IN_DOUBLE_WORD 0xf0f0f0f0
#define ZLIB_MAX_DICT_SIZE (1ULL << 15)

static void
process_zlib_deflate_chksum(struct rte_comp_op *op,
		z_stream *strm, enum rte_comp_checksum_type chksum)
{
	uint32_t dictionary_len = 0;
	uint8_t *dictionary = malloc(ZLIB_MAX_DICT_SIZE);
	uint32_t dictionary_start, dictionary_end, sum;
	uint8_t *sum_bytes = (uint8_t *)&sum;
	op->status = RTE_COMP_OP_STATUS_SUCCESS;

	switch (chksum) {
	case RTE_COMP_CHECKSUM_3GPP_PDCP_UDC:

		if (!dictionary) {
			ZLIB_PMD_ERR("Unable to fetch dictionary");
			op->status = RTE_COMP_OP_STATUS_ERROR;
			return;
		}

		if (deflateGetDictionary(strm, dictionary, &dictionary_len)) {
			ZLIB_PMD_ERR("Unable to fetch dictionary");
			op->status = RTE_COMP_OP_STATUS_CHECKSUM_VALIDATION_FAILED;
			free(dictionary);
			return;
		}

		dictionary_start = (uint32_t)(*dictionary);
		dictionary_end = (uint32_t)(*(dictionary + dictionary_len - 4));
		sum = (dictionary_start & BOTTOM_NIBBLE_OF_BYTES_IN_DOUBLE_WORD)
			+ (dictionary_start & (TOP_NIBBLE_OF_BYTE_IN_DOUBLE_WORD >> 4))
			+ (dictionary_end & BOTTOM_NIBBLE_OF_BYTES_IN_DOUBLE_WORD)
			+ (dictionary_end & (TOP_NIBBLE_OF_BYTE_IN_DOUBLE_WORD >> 4));

		op->output_chksum = ~(sum_bytes[0] + sum_bytes[1] + sum_bytes[2] + sum_bytes[3])
			 & BOTTOM_NIBBLE_OF_BYTE;
		break;
	case RTE_COMP_CHECKSUM_NONE:
		break;
	case RTE_COMP_CHECKSUM_CRC32:
	case RTE_COMP_CHECKSUM_ADLER32:
	case RTE_COMP_CHECKSUM_CRC32_ADLER32:
	default:
		ZLIB_PMD_ERR("Checksum not supported");
		op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
		free(dictionary);
		return;
	}
	free(dictionary);
}

static void
process_zlib_inflate_chksum(struct rte_comp_op *op,
		z_stream *strm,
		enum rte_comp_checksum_type chksum)
{
	uint32_t dictionary_len = 0;
	uint8_t *dictionary = malloc(ZLIB_MAX_DICT_SIZE);
	uint32_t dictionary_start, dictionary_end, sum;
	uint8_t *sum_bytes = (uint8_t *)&sum;
	op->status = RTE_COMP_OP_STATUS_SUCCESS;

	switch (chksum) {
	case RTE_COMP_CHECKSUM_3GPP_PDCP_UDC:
		if (!dictionary) {
			ZLIB_PMD_ERR("Unable to malloc dictionary");
			op->status = RTE_COMP_OP_STATUS_ERROR;
			return;
		}

		if (inflateGetDictionary(strm, dictionary, &dictionary_len)) {
			ZLIB_PMD_ERR("Unable to fetch dictionary");
			op->status = RTE_COMP_OP_STATUS_CHECKSUM_VALIDATION_FAILED;
			free(dictionary);
			return;
		}

		dictionary_start = (uint32_t)(*dictionary);
		dictionary_end = (uint32_t)(*(dictionary + dictionary_len - 4));
		sum = (dictionary_start & BOTTOM_NIBBLE_OF_BYTES_IN_DOUBLE_WORD)
			+ (dictionary_start & (TOP_NIBBLE_OF_BYTE_IN_DOUBLE_WORD >> 4))
			+ (dictionary_end & BOTTOM_NIBBLE_OF_BYTES_IN_DOUBLE_WORD)
			+ (dictionary_end & (TOP_NIBBLE_OF_BYTE_IN_DOUBLE_WORD >> 4));

		op->output_chksum = ~(sum_bytes[0] + sum_bytes[1] + sum_bytes[2] + sum_bytes[3])
			 & BOTTOM_NIBBLE_OF_BYTE;

		if (op->input_chksum != op->output_chksum) {
			ZLIB_PMD_ERR("Checksum does not match");
			op->status = RTE_COMP_OP_STATUS_CHECKSUM_VALIDATION_FAILED;
			free(dictionary);
			return;
		}
		break;
	case RTE_COMP_CHECKSUM_NONE:
		break;
	case RTE_COMP_CHECKSUM_CRC32:
	case RTE_COMP_CHECKSUM_ADLER32:
	case RTE_COMP_CHECKSUM_CRC32_ADLER32:
	default:
		ZLIB_PMD_ERR("Checksum not supported");
		op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
		free(dictionary);
		return;
	}
	free(dictionary);
}

static void
process_zlib_deflate(struct rte_comp_op *op, z_stream *strm)
{
	int ret, flush, fin_flush;
	unsigned long total_in_at_start, total_out_at_start;
	struct rte_mbuf *mbuf_src = op->m_src;
	struct rte_mbuf *mbuf_dst = op->m_dst;

	switch (op->flush_flag) {
	case RTE_COMP_FLUSH_FULL:
	case RTE_COMP_FLUSH_FINAL:
		fin_flush = Z_FINISH;
		break;
	case RTE_COMP_FLUSH_SYNC:
		fin_flush = Z_SYNC_FLUSH;
		break;
	default:
		op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
		ZLIB_PMD_ERR("Invalid flush value");
		return;
	}

	if (unlikely(!strm)) {
		op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
		ZLIB_PMD_ERR("Invalid z_stream");
		return;
	}
	/* Update z_stream with the inputs provided by application */
	strm->next_in = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
			op->src.offset);

	strm->avail_in = rte_pktmbuf_data_len(mbuf_src) - op->src.offset;

	strm->next_out = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
			op->dst.offset);

	strm->avail_out = rte_pktmbuf_data_len(mbuf_dst) - op->dst.offset;

	total_in_at_start = strm->total_in;
	total_out_at_start = strm->total_out;

	/* Set flush value to NO_FLUSH unless it is last mbuf */
	flush = Z_NO_FLUSH;
	/* Initialize status to SUCCESS */
	op->status = RTE_COMP_OP_STATUS_SUCCESS;

	do {
		/* Set flush value to Z_FINISH for last block */
		if ((op->src.length - (strm->total_in - total_in_at_start)) <= strm->avail_in) {
			strm->avail_in = (op->src.length - (strm->total_in - total_in_at_start));
			flush = fin_flush;
		}
		do {
			ret = deflate(strm, flush);
			if (unlikely(ret == Z_STREAM_ERROR)) {
				/* error return, do not process further */
				op->status =  RTE_COMP_OP_STATUS_ERROR;
				goto def_end;
			}
			/* Break if Z_STREAM_END is encountered */
			if (ret == Z_STREAM_END)
				goto def_end;

		/* Keep looping until input mbuf is consumed.
		 * Exit if destination mbuf gets exhausted.
		 */
		} while ((strm->avail_out == 0) &&
			COMPUTE_BUF(mbuf_dst, strm->next_out, strm->avail_out));

		if (!strm->avail_out) {
			/* there is no space for compressed output */
			op->status = RTE_COMP_OP_STATUS_OUT_OF_SPACE_TERMINATED;
			break;
		}

	/* Update source buffer to next mbuf
	 * Exit if input buffers are fully consumed
	 */
	} while (COMPUTE_BUF(mbuf_src, strm->next_in, strm->avail_in));

def_end:
	/* Update op stats */
	switch (op->status) {
	case RTE_COMP_OP_STATUS_SUCCESS:
		op->consumed += strm->total_in - total_in_at_start;
	/* Fall-through */
	case RTE_COMP_OP_STATUS_OUT_OF_SPACE_TERMINATED:
		op->produced += strm->total_out - total_out_at_start;
		break;
	default:
		ZLIB_PMD_ERR("stats not updated for status:%d",
				op->status);
	}

	if (op->flush_flag != RTE_COMP_FLUSH_SYNC)
		deflateReset(strm);
}

static void
process_zlib_inflate(struct rte_comp_op *op, z_stream *strm)
{
	int ret, flush;
	unsigned long total_in_at_start, total_out_at_start;
	struct rte_mbuf *mbuf_src = op->m_src;
	struct rte_mbuf *mbuf_dst = op->m_dst;

	if (unlikely(!strm)) {
		op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
		ZLIB_PMD_ERR("Invalid z_stream");
		return;
	}
	strm->next_in = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
			op->src.offset);

	strm->avail_in = rte_pktmbuf_data_len(mbuf_src) - op->src.offset;

	strm->next_out = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
			op->dst.offset);

	strm->avail_out = rte_pktmbuf_data_len(mbuf_dst) - op->dst.offset;

	total_in_at_start = strm->total_in;
	total_out_at_start = strm->total_out;

	/** Ignoring flush value provided from application for decompression */
	flush = Z_NO_FLUSH;
	/* initialize status to SUCCESS */
	op->status = RTE_COMP_OP_STATUS_SUCCESS;

	do {
		do {
			ret = inflate(strm, flush);

			switch (ret) {
			/* Fall-through */
			case Z_NEED_DICT:
				ret = Z_DATA_ERROR;
			/* Fall-through */
			case Z_DATA_ERROR:
			/* Fall-through */
			case Z_MEM_ERROR:
			/* Fall-through */
			case Z_STREAM_ERROR:
				op->status = RTE_COMP_OP_STATUS_ERROR;
			/* Fall-through */
			case Z_STREAM_END:
				/* no further computation needed if
				 * Z_STREAM_END is encountered
				 */
				goto inf_end;
			default:
				/* success */
				break;

			}
		/* Keep looping until input mbuf is consumed.
		 * Exit if destination mbuf gets exhausted.
		 */
		} while ((strm->avail_out == 0) &&
			COMPUTE_BUF(mbuf_dst, strm->next_out, strm->avail_out));

		if (!strm->avail_out) {
			/* there is no more space for decompressed output */
			op->status = RTE_COMP_OP_STATUS_OUT_OF_SPACE_TERMINATED;
			break;
		}
	/* Read next input buffer to be processed, exit if compressed
	 * blocks are fully read
	 */
	} while (COMPUTE_BUF(mbuf_src, strm->next_in, strm->avail_in));

inf_end:
	/* Update op stats */
	switch (op->status) {
	case RTE_COMP_OP_STATUS_SUCCESS:
		op->consumed += strm->total_in - total_in_at_start;
	/* Fall-through */
	case RTE_COMP_OP_STATUS_OUT_OF_SPACE_TERMINATED:
		op->produced += strm->total_out - total_out_at_start;
		break;
	default:
		ZLIB_PMD_ERR("stats not produced for status:%d",
				op->status);
	}

	if (op->flush_flag != RTE_COMP_FLUSH_SYNC)
		inflateReset(strm);
}

/** Process comp operation for mbuf */
static inline int
process_zlib_op(struct zlib_qp *qp, struct rte_comp_op *op)
{
	struct zlib_stream *stream;
	struct zlib_priv_xform *private_xform;

	if ((op->op_type == RTE_COMP_OP_STATEFUL) ||
			(op->src.offset > rte_pktmbuf_data_len(op->m_src)) ||
			(op->dst.offset > rte_pktmbuf_data_len(op->m_dst))) {
		op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
		ZLIB_PMD_ERR("Invalid source or destination buffers or "
				  "invalid Operation requested");
	} else {
		private_xform = (struct zlib_priv_xform *)op->private_xform;
		stream = &private_xform->stream;
		stream->chksum(op, &stream->strm, stream->chksum_type);
		if (op->status != RTE_COMP_OP_STATUS_SUCCESS)
			return -1;

		stream->comp(op, &stream->strm);
	}
	/* whatever is out of op, put it into completion queue with
	 * its status
	 */
	return rte_ring_enqueue(qp->processed_pkts, (void *)op);
}

/** Parse comp xform and set private xform/Stream parameters */
int
zlib_set_stream_parameters(const struct rte_comp_xform *xform,
		struct zlib_stream *stream)
{
	int strategy, level, wbits;
	z_stream *strm = &stream->strm;

	/* allocate deflate state */
	strm->zalloc = Z_NULL;
	strm->zfree = Z_NULL;
	strm->opaque = Z_NULL;

	switch (xform->type) {
	case RTE_COMP_COMPRESS:
		stream->comp = process_zlib_deflate;
		stream->free = deflateEnd;
		stream->chksum = process_zlib_deflate_chksum;
		/** Compression window bits */
		switch (xform->compress.algo) {
		case RTE_COMP_ALGO_DEFLATE:
			wbits = -(xform->compress.window_size);
			break;
		default:
			ZLIB_PMD_ERR("Compression algorithm not supported");
			return -1;
		}
		/** Compression Level */
		switch (xform->compress.level) {
		case RTE_COMP_LEVEL_PMD_DEFAULT:
			level = Z_DEFAULT_COMPRESSION;
			break;
		case RTE_COMP_LEVEL_NONE:
			level = Z_NO_COMPRESSION;
			break;
		case RTE_COMP_LEVEL_MIN:
			level = Z_BEST_SPEED;
			break;
		case RTE_COMP_LEVEL_MAX:
			level = Z_BEST_COMPRESSION;
			break;
		default:
			level = xform->compress.level;
			if (level < RTE_COMP_LEVEL_MIN ||
					level > RTE_COMP_LEVEL_MAX) {
				ZLIB_PMD_ERR("Compression level %d "
						"not supported",
						level);
				return -1;
			}
			break;
		}
		/** Compression strategy */
		switch (xform->compress.deflate.huffman) {
		case RTE_COMP_HUFFMAN_DEFAULT:
			strategy = Z_DEFAULT_STRATEGY;
			break;
		case RTE_COMP_HUFFMAN_FIXED:
			strategy = Z_FIXED;
			break;
		case RTE_COMP_HUFFMAN_DYNAMIC:
			strategy = Z_DEFAULT_STRATEGY;
			break;
		default:
			ZLIB_PMD_ERR("Compression strategy not supported");
			return -1;
		}

		/** Checksum used */
		stream->chksum_type = xform->compress.chksum;

		if (deflateInit2(strm, level,
					Z_DEFLATED, wbits,
					DEF_MEM_LEVEL, strategy) != Z_OK) {
			ZLIB_PMD_ERR("Deflate init failed");
			return -1;
		}

		if (xform->compress.deflate.dictionary) {
			if (deflateSetDictionary(strm, xform->compress.deflate.dictionary,
					xform->compress.deflate.dictionary_len)) {
				ZLIB_PMD_ERR("Deflate set dictionary failed");
				return -1;
			}
		}
		break;

	case RTE_COMP_DECOMPRESS:
		stream->comp = process_zlib_inflate;
		stream->free = inflateEnd;
		stream->chksum = process_zlib_inflate_chksum;
		/** window bits */
		switch (xform->decompress.algo) {
		case RTE_COMP_ALGO_DEFLATE:
			wbits = -(xform->decompress.window_size);
			break;
		default:
			ZLIB_PMD_ERR("Compression algorithm not supported");
			return -1;
		}

		/** Checksum used */
		stream->chksum_type = xform->decompress.chksum;

		if (inflateInit2(strm, wbits) != Z_OK) {
			ZLIB_PMD_ERR("Inflate init failed");
			return -1;
		}

		if (xform->decompress.inflate.dictionary) {
			if (inflateSetDictionary(strm, xform->decompress.inflate.dictionary,
					xform->decompress.inflate.dictionary_len)) {
				ZLIB_PMD_ERR("inflate set dictionary failed");
				return -1;
			}
		}
		break;
	default:
		return -1;
	}
	return 0;
}

static uint16_t
zlib_pmd_enqueue_burst(void *queue_pair,
			struct rte_comp_op **ops, uint16_t nb_ops)
{
	struct zlib_qp *qp = queue_pair;
	int ret;
	uint16_t i;
	uint16_t enqd = 0;
	for (i = 0; i < nb_ops; i++) {
		ret = process_zlib_op(qp, ops[i]);
		if (unlikely(ret < 0)) {
			/* increment count if failed to push to completion
			 * queue
			 */
			qp->qp_stats.enqueue_err_count++;
		} else {
			qp->qp_stats.enqueued_count++;
			enqd++;
		}
	}
	return enqd;
}

static uint16_t
zlib_pmd_dequeue_burst(void *queue_pair,
			struct rte_comp_op **ops, uint16_t nb_ops)
{
	struct zlib_qp *qp = queue_pair;

	unsigned int nb_dequeued = 0;

	nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts,
			(void **)ops, nb_ops, NULL);
	qp->qp_stats.dequeued_count += nb_dequeued;

	return nb_dequeued;
}

static int
zlib_create(const char *name,
		struct rte_vdev_device *vdev,
		struct rte_compressdev_pmd_init_params *init_params)
{
	struct rte_compressdev *dev;

	dev = rte_compressdev_pmd_create(name, &vdev->device,
			sizeof(struct zlib_private), init_params);
	if (dev == NULL) {
		ZLIB_PMD_ERR("driver %s: create failed", init_params->name);
		return -ENODEV;
	}

	dev->dev_ops = rte_zlib_pmd_ops;

	/* register rx/tx burst functions for data path */
	dev->dequeue_burst = zlib_pmd_dequeue_burst;
	dev->enqueue_burst = zlib_pmd_enqueue_burst;

	return 0;
}

static int
zlib_probe(struct rte_vdev_device *vdev)
{
	struct rte_compressdev_pmd_init_params init_params = {
		"",
		rte_socket_id()
	};
	const char *name;
	const char *input_args;
	int retval;

	name = rte_vdev_device_name(vdev);

	if (name == NULL)
		return -EINVAL;

	input_args = rte_vdev_device_args(vdev);

	retval = rte_compressdev_pmd_parse_input_args(&init_params, input_args);
	if (retval < 0) {
		ZLIB_PMD_LOG(ERR,
			"Failed to parse initialisation arguments[%s]",
			input_args);
		return -EINVAL;
	}

	return zlib_create(name, vdev, &init_params);
}

static int
zlib_remove(struct rte_vdev_device *vdev)
{
	struct rte_compressdev *compressdev;
	const char *name;

	name = rte_vdev_device_name(vdev);
	if (name == NULL)
		return -EINVAL;

	compressdev = rte_compressdev_pmd_get_named_dev(name);
	if (compressdev == NULL)
		return -ENODEV;

	return rte_compressdev_pmd_destroy(compressdev);
}

static struct rte_vdev_driver zlib_pmd_drv = {
	.probe = zlib_probe,
	.remove = zlib_remove
};

RTE_PMD_REGISTER_VDEV(COMPRESSDEV_NAME_ZLIB_PMD, zlib_pmd_drv);
RTE_LOG_REGISTER_DEFAULT(zlib_logtype_driver, INFO);