DPDK logo

Elixir Cross Referencer

  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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2018 Intel Corporation
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/wait.h>

#include <rte_common.h>
#include <rte_debug.h>
#include <rte_eal.h>
#include <rte_errno.h>
#include <rte_malloc.h>
#include <rte_ring.h>
#include <rte_string_fns.h>

#include "test.h"

#define EXTERNAL_MEM_SZ (RTE_PGSIZE_4K << 10) /* 4M of data */

static int
check_mem(void *addr, rte_iova_t *iova, size_t pgsz, int n_pages)
{
	int i;

	/* check that we can get this memory from EAL now */
	for (i = 0; i < n_pages; i++) {
		const struct rte_memseg_list *msl;
		const struct rte_memseg *ms;
		void *cur = RTE_PTR_ADD(addr, pgsz * i);
		rte_iova_t expected_iova;

		msl = rte_mem_virt2memseg_list(cur);
		if (!msl->external) {
			printf("%s():%i: Memseg list is not marked as external\n",
				__func__, __LINE__);
			return -1;
		}

		ms = rte_mem_virt2memseg(cur, msl);
		if (ms == NULL) {
			printf("%s():%i: Failed to retrieve memseg for external mem\n",
				__func__, __LINE__);
			return -1;
		}
		if (ms->addr != cur) {
			printf("%s():%i: VA mismatch\n", __func__, __LINE__);
			return -1;
		}
		expected_iova = (iova == NULL) ? RTE_BAD_IOVA : iova[i];
		if (ms->iova != expected_iova) {
			printf("%s():%i: IOVA mismatch\n", __func__, __LINE__);
			return -1;
		}
	}
	return 0;
}

static int
test_malloc_invalid_param(void *addr, size_t len, size_t pgsz, rte_iova_t *iova,
		int n_pages)
{
	static const char * const names[] = {
		NULL, /* NULL name */
		"",   /* empty name */
		"this heap name is definitely way too long to be valid"
	};
	const char *valid_name = "valid heap name";
	unsigned int i;

	/* check invalid name handling */
	for (i = 0; i < RTE_DIM(names); i++) {
		const char *name = names[i];

		/* these calls may fail for other reasons, so check errno */
		if (rte_malloc_heap_create(name) >= 0 || rte_errno != EINVAL) {
			printf("%s():%i: Created heap with invalid name\n",
					__func__, __LINE__);
			goto fail;
		}

		if (rte_malloc_heap_destroy(name) >= 0 || rte_errno != EINVAL) {
			printf("%s():%i: Destroyed heap with invalid name\n",
					__func__, __LINE__);
			goto fail;
		}

		if (rte_malloc_heap_get_socket(name) >= 0 ||
				rte_errno != EINVAL) {
			printf("%s():%i: Found socket for heap with invalid name\n",
					__func__, __LINE__);
			goto fail;
		}

		if (rte_malloc_heap_memory_add(name, addr, len,
				NULL, 0, pgsz) >= 0 || rte_errno != EINVAL) {
			printf("%s():%i: Added memory to heap with invalid name\n",
					__func__, __LINE__);
			goto fail;
		}
		if (rte_malloc_heap_memory_remove(name, addr, len) >= 0 ||
				rte_errno != EINVAL) {
			printf("%s():%i: Removed memory from heap with invalid name\n",
					__func__, __LINE__);
			goto fail;
		}

		if (rte_malloc_heap_memory_attach(name, addr, len) >= 0 ||
				rte_errno != EINVAL) {
			printf("%s():%i: Attached memory to heap with invalid name\n",
				__func__, __LINE__);
			goto fail;
		}
		if (rte_malloc_heap_memory_detach(name, addr, len) >= 0 ||
				rte_errno != EINVAL) {
			printf("%s():%i: Detached memory from heap with invalid name\n",
				__func__, __LINE__);
			goto fail;
		}
	}

	/* do same as above, but with a valid heap name */

	/* skip create call */
	if (rte_malloc_heap_destroy(valid_name) >= 0 || rte_errno != ENOENT) {
		printf("%s():%i: Destroyed heap with invalid name\n",
			__func__, __LINE__);
		goto fail;
	}
	if (rte_malloc_heap_get_socket(valid_name) >= 0 ||
			rte_errno != ENOENT) {
		printf("%s():%i: Found socket for heap with invalid name\n",
				__func__, __LINE__);
		goto fail;
	}

	/* these calls may fail for other reasons, so check errno */
	if (rte_malloc_heap_memory_add(valid_name, addr, len,
			NULL, 0, pgsz) >= 0 || rte_errno != ENOENT) {
		printf("%s():%i: Added memory to non-existent heap\n",
			__func__, __LINE__);
		goto fail;
	}
	if (rte_malloc_heap_memory_remove(valid_name, addr, len) >= 0 ||
			rte_errno != ENOENT) {
		printf("%s():%i: Removed memory from non-existent heap\n",
			__func__, __LINE__);
		goto fail;
	}

	if (rte_malloc_heap_memory_attach(valid_name, addr, len) >= 0 ||
			rte_errno != ENOENT) {
		printf("%s():%i: Attached memory to non-existent heap\n",
			__func__, __LINE__);
		goto fail;
	}
	if (rte_malloc_heap_memory_detach(valid_name, addr, len) >= 0 ||
			rte_errno != ENOENT) {
		printf("%s():%i: Detached memory from non-existent heap\n",
			__func__, __LINE__);
		goto fail;
	}

	/* create a valid heap but test other invalid parameters */
	if (rte_malloc_heap_create(valid_name) != 0) {
		printf("%s():%i: Failed to create valid heap\n",
			__func__, __LINE__);
		goto fail;
	}

	/* zero length */
	if (rte_malloc_heap_memory_add(valid_name, addr, 0,
			NULL, 0, pgsz) >= 0 || rte_errno != EINVAL) {
		printf("%s():%i: Added memory with invalid parameters\n",
			__func__, __LINE__);
		goto fail;
	}

	if (rte_malloc_heap_memory_remove(valid_name, addr, 0) >= 0 ||
			rte_errno != EINVAL) {
		printf("%s():%i: Removed memory with invalid parameters\n",
			__func__, __LINE__);
		goto fail;
	}

	if (rte_malloc_heap_memory_attach(valid_name, addr, 0) >= 0 ||
			rte_errno != EINVAL) {
		printf("%s():%i: Attached memory with invalid parameters\n",
			__func__, __LINE__);
		goto fail;
	}
	if (rte_malloc_heap_memory_detach(valid_name, addr, 0) >= 0 ||
			rte_errno != EINVAL) {
		printf("%s():%i: Detached memory with invalid parameters\n",
			__func__, __LINE__);
		goto fail;
	}

	/* zero address */
	if (rte_malloc_heap_memory_add(valid_name, NULL, len,
			NULL, 0, pgsz) >= 0 || rte_errno != EINVAL) {
		printf("%s():%i: Added memory with invalid parameters\n",
			__func__, __LINE__);
		goto fail;
	}

	if (rte_malloc_heap_memory_remove(valid_name, NULL, len) >= 0 ||
			rte_errno != EINVAL) {
		printf("%s():%i: Removed memory with invalid parameters\n",
			__func__, __LINE__);
		goto fail;
	}

	if (rte_malloc_heap_memory_attach(valid_name, NULL, len) >= 0 ||
			rte_errno != EINVAL) {
		printf("%s():%i: Attached memory with invalid parameters\n",
			__func__, __LINE__);
		goto fail;
	}
	if (rte_malloc_heap_memory_detach(valid_name, NULL, len) >= 0 ||
			rte_errno != EINVAL) {
		printf("%s():%i: Detached memory with invalid parameters\n",
			__func__, __LINE__);
		goto fail;
	}

	/* the following tests are only valid if IOVA table is not NULL */
	if (iova != NULL) {
		/* wrong page count */
		if (rte_malloc_heap_memory_add(valid_name, addr, len,
				iova, 0, pgsz) >= 0 || rte_errno != EINVAL) {
			printf("%s():%i: Added memory with invalid parameters\n",
				__func__, __LINE__);
			goto fail;
		}
		if (rte_malloc_heap_memory_add(valid_name, addr, len,
				iova, n_pages - 1, pgsz) >= 0 ||
				rte_errno != EINVAL) {
			printf("%s():%i: Added memory with invalid parameters\n",
				__func__, __LINE__);
			goto fail;
		}
		if (rte_malloc_heap_memory_add(valid_name, addr, len,
				iova, n_pages + 1, pgsz) >= 0 ||
				rte_errno != EINVAL) {
			printf("%s():%i: Added memory with invalid parameters\n",
				__func__, __LINE__);
			goto fail;
		}
	}

	/* tests passed, destroy heap */
	if (rte_malloc_heap_destroy(valid_name) != 0) {
		printf("%s():%i: Failed to destroy valid heap\n",
			__func__, __LINE__);
		goto fail;
	}
	return 0;
fail:
	rte_malloc_heap_destroy(valid_name);
	return -1;
}

static int
test_malloc_basic(void *addr, size_t len, size_t pgsz, rte_iova_t *iova,
		int n_pages)
{
	const char *heap_name = "heap";
	void *ptr = NULL;
	int socket_id;
	const struct rte_memzone *mz = NULL, *contig_mz = NULL;

	/* create heap */
	if (rte_malloc_heap_create(heap_name) != 0) {
		printf("%s():%i: Failed to create malloc heap\n",
			__func__, __LINE__);
		goto fail;
	}

	/* get socket ID corresponding to this heap */
	socket_id = rte_malloc_heap_get_socket(heap_name);
	if (socket_id < 0) {
		printf("%s():%i: cannot find socket for external heap\n",
			__func__, __LINE__);
		goto fail;
	}

	/* heap is empty, so any allocation should fail */
	ptr = rte_malloc_socket("EXTMEM", 64, 0, socket_id);
	if (ptr != NULL) {
		printf("%s():%i: Allocated from empty heap\n", __func__,
			__LINE__);
		goto fail;
	}

	/* add memory to heap */
	if (rte_malloc_heap_memory_add(heap_name, addr, len,
			iova, n_pages, pgsz) != 0) {
		printf("%s():%i: Failed to add memory to heap\n",
			__func__, __LINE__);
		goto fail;
	}

	/* check if memory is accessible from EAL */
	if (check_mem(addr, iova, pgsz, n_pages) < 0)
		goto fail;

	/* allocate - this now should succeed */
	ptr = rte_malloc_socket("EXTMEM", 64, 0, socket_id);
	if (ptr == NULL) {
		printf("%s():%i: Failed to allocate from external heap\n",
			__func__, __LINE__);
		goto fail;
	}

	/* check if address is in expected range */
	if (ptr < addr || ptr >= RTE_PTR_ADD(addr, len)) {
		printf("%s():%i: Allocated from unexpected address space\n",
			__func__, __LINE__);
		goto fail;
	}

	/* we've allocated something - removing memory should fail */
	if (rte_malloc_heap_memory_remove(heap_name, addr, len) >= 0 ||
			rte_errno != EBUSY) {
		printf("%s():%i: Removing memory succeeded when memory is not free\n",
			__func__, __LINE__);
		goto fail;
	}
	if (rte_malloc_heap_destroy(heap_name) >= 0 || rte_errno != EBUSY) {
		printf("%s():%i: Destroying heap succeeded when memory is not free\n",
			__func__, __LINE__);
		goto fail;
	}

	/* try allocating a memzone */
	mz = rte_memzone_reserve("heap_test", pgsz * 2, socket_id, 0);
	if (mz == NULL) {
		printf("%s():%i: Failed to reserve memzone\n",
			__func__, __LINE__);
		goto fail;
	}
	/* try allocating an IOVA-contiguous memzone - this should succeed
	 * if we've set up a contiguous IOVA table, and fail if we haven't.
	 */
	contig_mz = rte_memzone_reserve("heap_test_contig", pgsz * 2, socket_id,
			RTE_MEMZONE_IOVA_CONTIG);
	if ((iova == NULL) != (contig_mz == NULL)) {
		printf("%s():%i: Failed to reserve memzone\n",
			__func__, __LINE__);
		goto fail;
	}

	rte_malloc_dump_stats(stdout, NULL);
	rte_malloc_dump_heaps(stdout);

	/* free memory - removing it should now succeed */
	rte_free(ptr);
	ptr = NULL;

	rte_memzone_free(mz);
	mz = NULL;
	rte_memzone_free(contig_mz);
	contig_mz = NULL;

	if (rte_malloc_heap_memory_remove(heap_name, addr, len) != 0) {
		printf("%s():%i: Removing memory from heap failed\n",
			__func__, __LINE__);
		goto fail;
	}
	if (rte_malloc_heap_destroy(heap_name) != 0) {
		printf("%s():%i: Destroying heap failed\n",
			__func__, __LINE__);
		goto fail;
	}

	return 0;
fail:
	rte_memzone_free(contig_mz);
	rte_memzone_free(mz);
	rte_free(ptr);
	/* even if something failed, attempt to clean up */
	rte_malloc_heap_memory_remove(heap_name, addr, len);
	rte_malloc_heap_destroy(heap_name);

	return -1;
}

static int
test_extmem_invalid_param(void *addr, size_t len, size_t pgsz, rte_iova_t *iova,
		int n_pages)
{
	/* these calls may fail for other reasons, so check errno */
	if (rte_extmem_unregister(addr, len) >= 0 ||
			rte_errno != ENOENT) {
		printf("%s():%i: Unregistered non-existent memory\n",
			__func__, __LINE__);
		return -1;
	}

	if (rte_extmem_attach(addr, len) >= 0 ||
			rte_errno != ENOENT) {
		printf("%s():%i: Attached to non-existent memory\n",
			__func__, __LINE__);
		return -1;
	}
	if (rte_extmem_attach(addr, len) >= 0 ||
			rte_errno != ENOENT) {
		printf("%s():%i: Detached from non-existent memory\n",
			__func__, __LINE__);
		return -1;
	}

	/* zero length */
	if (rte_extmem_register(addr, 0, NULL, 0, pgsz) >= 0 ||
			rte_errno != EINVAL) {
		printf("%s():%i: Registered memory with invalid parameters\n",
			__func__, __LINE__);
		return -1;
	}

	if (rte_extmem_unregister(addr, 0) >= 0 ||
			rte_errno != EINVAL) {
		printf("%s():%i: Unregistered memory with invalid parameters\n",
			__func__, __LINE__);
		return -1;
	}

	if (rte_extmem_attach(addr, 0) >= 0 ||
			rte_errno != EINVAL) {
		printf("%s():%i: Attached memory with invalid parameters\n",
			__func__, __LINE__);
		return -1;
	}
	if (rte_extmem_attach(addr, 0) >= 0 ||
			rte_errno != EINVAL) {
		printf("%s():%i: Detached memory with invalid parameters\n",
			__func__, __LINE__);
		return -1;
	}

	/* zero address */
	if (rte_extmem_register(NULL, len, NULL, 0, pgsz) >= 0 ||
			rte_errno != EINVAL) {
		printf("%s():%i: Registered memory with invalid parameters\n",
			__func__, __LINE__);
		return -1;
	}

	if (rte_extmem_unregister(NULL, len) >= 0 ||
			rte_errno != EINVAL) {
		printf("%s():%i: Unregistered memory with invalid parameters\n",
			__func__, __LINE__);
		return -1;
	}

	if (rte_extmem_attach(NULL, len) >= 0 ||
			rte_errno != EINVAL) {
		printf("%s():%i: Attached memory with invalid parameters\n",
			__func__, __LINE__);
		return -1;
	}
	if (rte_extmem_attach(NULL, len) >= 0 ||
			rte_errno != EINVAL) {
		printf("%s():%i: Detached memory with invalid parameters\n",
			__func__, __LINE__);
		return -1;
	}

	/* the following tests are only valid if IOVA table is not NULL */
	if (iova != NULL) {
		/* wrong page count */
		if (rte_extmem_register(addr, len,
				iova, 0, pgsz) >= 0 || rte_errno != EINVAL) {
			printf("%s():%i: Registered memory with invalid parameters\n",
				__func__, __LINE__);
			return -1;
		}
		if (rte_extmem_register(addr, len,
				iova, n_pages - 1, pgsz) >= 0 ||
				rte_errno != EINVAL) {
			printf("%s():%i: Registered memory with invalid parameters\n",
				__func__, __LINE__);
			return -1;
		}
		if (rte_extmem_register(addr, len,
				iova, n_pages + 1, pgsz) >= 0 ||
				rte_errno != EINVAL) {
			printf("%s():%i: Registered memory with invalid parameters\n",
				__func__, __LINE__);
			return -1;
		}
	}

	return 0;
}

static int
test_extmem_basic(void *addr, size_t len, size_t pgsz, rte_iova_t *iova,
		int n_pages)
{
	/* register memory */
	if (rte_extmem_register(addr, len, iova, n_pages, pgsz) != 0) {
		printf("%s():%i: Failed to register memory\n",
			__func__, __LINE__);
		goto fail;
	}

	/* check if memory is accessible from EAL */
	if (check_mem(addr, iova, pgsz, n_pages) < 0)
		goto fail;

	if (rte_extmem_unregister(addr, len) != 0) {
		printf("%s():%i: Removing memory from heap failed\n",
			__func__, __LINE__);
		goto fail;
	}

	return 0;
fail:
	/* even if something failed, attempt to clean up */
	rte_extmem_unregister(addr, len);

	return -1;
}

/* we need to test attach/detach in secondary processes. */
static int
test_external_mem(void)
{
	size_t len = EXTERNAL_MEM_SZ;
	size_t pgsz = RTE_PGSIZE_4K;
	rte_iova_t iova[len / pgsz];
	void *addr;
	int ret, n_pages;
	int i;

	/* create external memory area */
	n_pages = RTE_DIM(iova);
	addr = mmap(NULL, len, PROT_WRITE | PROT_READ,
			MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
	if (addr == MAP_FAILED) {
		printf("%s():%i: Failed to create dummy memory area\n",
			__func__, __LINE__);
		return -1;
	}
	for (i = 0; i < n_pages; i++) {
		/* arbitrary IOVA */
		rte_iova_t tmp = 0x100000000 + i * pgsz;
		iova[i] = tmp;
	}

	/* test external heap memory */
	ret = test_malloc_invalid_param(addr, len, pgsz, iova, n_pages);
	ret |= test_malloc_basic(addr, len, pgsz, iova, n_pages);
	/* when iova table is NULL, everything should still work */
	ret |= test_malloc_invalid_param(addr, len, pgsz, NULL, n_pages);
	ret |= test_malloc_basic(addr, len, pgsz, NULL, n_pages);

	/* test non-heap memory */
	ret |= test_extmem_invalid_param(addr, len, pgsz, iova, n_pages);
	ret |= test_extmem_basic(addr, len, pgsz, iova, n_pages);
	/* when iova table is NULL, everything should still work */
	ret |= test_extmem_invalid_param(addr, len, pgsz, NULL, n_pages);
	ret |= test_extmem_basic(addr, len, pgsz, NULL, n_pages);

	munmap(addr, len);

	return ret;
}

REGISTER_TEST_COMMAND(external_mem_autotest, test_external_mem);