aboutsummaryrefslogtreecommitdiffstats
path: root/app/test/test_ivshmem.c
blob: ae9fd6cccc0a3f79f19f1447ed269475b0ec23ea (plain)
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
/*-
 *   BSD LICENSE
 *
 *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Intel Corporation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <stdio.h>

#include <cmdline_parse.h>

#include "test.h"

#include <rte_common.h>
#include <rte_ivshmem.h>
#include <rte_string_fns.h>
#include "process.h"

#define DUPLICATE_METADATA "duplicate"
#define METADATA_NAME "metadata"
#define NONEXISTENT_METADATA "nonexistent"
#define FIRST_TEST 'a'

#define launch_proc(ARGV) process_dup(ARGV, \
		sizeof(ARGV)/(sizeof(ARGV[0])), "test_ivshmem")

#define ASSERT(cond,msg) do {						\
		if (!(cond)) {								\
			printf("**** TEST %s() failed: %s\n",	\
				__func__, msg);						\
			return -1;								\
		}											\
} while(0)

static char*
get_current_prefix(char * prefix, int size)
{
	char path[PATH_MAX] = {0};
	char buf[PATH_MAX] = {0};

	/* get file for config (fd is always 3) */
	snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);

	/* return NULL on error */
	if (readlink(path, buf, sizeof(buf)) == -1)
		return NULL;

	/* get the basename */
	snprintf(buf, sizeof(buf), "%s", basename(buf));

	/* copy string all the way from second char up to start of _config */
	snprintf(prefix, size, "%.*s",
			(int)(strnlen(buf, sizeof(buf)) - sizeof("_config")),
			&buf[1]);

	return prefix;
}

static struct rte_ivshmem_metadata*
mmap_metadata(const char *name)
{
	int fd;
	char pathname[PATH_MAX];
	struct rte_ivshmem_metadata *metadata;

	snprintf(pathname, sizeof(pathname),
			"/var/run/.dpdk_ivshmem_metadata_%s", name);

	fd = open(pathname, O_RDWR, 0660);
	if (fd < 0)
		return NULL;

	metadata = (struct rte_ivshmem_metadata*) mmap(NULL,
			sizeof(struct rte_ivshmem_metadata), PROT_READ | PROT_WRITE,
			MAP_SHARED, fd, 0);

	if (metadata == MAP_FAILED)
		return NULL;

	close(fd);

	return metadata;
}

static int
create_duplicate(void)
{
	/* create a metadata that another process will then try to overwrite */
	ASSERT (rte_ivshmem_metadata_create(DUPLICATE_METADATA) == 0,
			"Creating metadata failed");
	return 0;
}

static int
test_ivshmem_create_lots_of_memzones(void)
{
	int i;
	char name[IVSHMEM_NAME_LEN];
	const struct rte_memzone *mz;

	ASSERT(rte_ivshmem_metadata_create(METADATA_NAME) == 0,
			"Failed to create metadata");

	for (i = 0; i < RTE_LIBRTE_IVSHMEM_MAX_ENTRIES; i++) {
		snprintf(name, sizeof(name), "mz_%i", i);

		mz = rte_memzone_reserve(name, RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY, 0);
		ASSERT(mz != NULL, "Failed to reserve memzone");

		ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) == 0,
				"Failed to add memzone");
	}
	mz = rte_memzone_reserve("one too many", RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY, 0);
	ASSERT(mz != NULL, "Failed to reserve memzone");

	ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) < 0,
		"Metadata should have been full");

	return 0;
}

static int
test_ivshmem_create_duplicate_memzone(void)
{
	const struct rte_memzone *mz;

	ASSERT(rte_ivshmem_metadata_create(METADATA_NAME) == 0,
			"Failed to create metadata");

	mz = rte_memzone_reserve("mz", RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY, 0);
	ASSERT(mz != NULL, "Failed to reserve memzone");

	ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) == 0,
			"Failed to add memzone");

	ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) < 0,
			"Added the same memzone twice");

	return 0;
}

static int
test_ivshmem_api_test(void)
{
	const struct rte_memzone * mz;
	struct rte_mempool * mp;
	struct rte_ring * r;
	char buf[BUFSIZ];

	memset(buf, 0, sizeof(buf));

	r = rte_ring_create("ring", 1, SOCKET_ID_ANY, 0);
	mp = rte_mempool_create("mempool", 1, 1, 1, 1, NULL, NULL, NULL, NULL,
			SOCKET_ID_ANY, 0);
	mz = rte_memzone_reserve("memzone", 64, SOCKET_ID_ANY, 0);

	ASSERT(r != NULL, "Failed to create ring");
	ASSERT(mp != NULL, "Failed to create mempool");
	ASSERT(mz != NULL, "Failed to reserve memzone");

	/* try to create NULL metadata */
	ASSERT(rte_ivshmem_metadata_create(NULL) < 0,
			"Created metadata with NULL name");

	/* create valid metadata to do tests on */
	ASSERT(rte_ivshmem_metadata_create(METADATA_NAME) == 0,
			"Failed to create metadata");

	/* test adding memzone */
	ASSERT(rte_ivshmem_metadata_add_memzone(NULL, NULL) < 0,
			"Added NULL memzone to NULL metadata");
	ASSERT(rte_ivshmem_metadata_add_memzone(NULL, METADATA_NAME) < 0,
			"Added NULL memzone");
	ASSERT(rte_ivshmem_metadata_add_memzone(mz, NULL) < 0,
			"Added memzone to NULL metadata");
	ASSERT(rte_ivshmem_metadata_add_memzone(mz, NONEXISTENT_METADATA) < 0,
			"Added memzone to nonexistent metadata");

	/* test adding ring */
	ASSERT(rte_ivshmem_metadata_add_ring(NULL, NULL) < 0,
			"Added NULL ring to NULL metadata");
	ASSERT(rte_ivshmem_metadata_add_ring(NULL, METADATA_NAME) < 0,
			"Added NULL ring");
	ASSERT(rte_ivshmem_metadata_add_ring(r, NULL) < 0,
			"Added ring to NULL metadata");
	ASSERT(rte_ivshmem_metadata_add_ring(r, NONEXISTENT_METADATA) < 0,
			"Added ring to nonexistent metadata");

	/* test adding mempool */
	ASSERT(rte_ivshmem_metadata_add_mempool(NULL, NULL) < 0,
			"Added NULL mempool to NULL metadata");
	ASSERT(rte_ivshmem_metadata_add_mempool(NULL, METADATA_NAME) < 0,
			"Added NULL mempool");
	ASSERT(rte_ivshmem_metadata_add_mempool(mp, NULL) < 0,
			"Added mempool to NULL metadata");
	ASSERT(rte_ivshmem_metadata_add_mempool(mp, NONEXISTENT_METADATA) < 0,
			"Added mempool to nonexistent metadata");

	/* test creating command line */
	ASSERT(rte_ivshmem_metadata_cmdline_generate(NULL, sizeof(buf), METADATA_NAME) < 0,
			"Written command line into NULL buffer");
	ASSERT(strnlen(buf, sizeof(buf)) == 0, "Buffer is not empty");

	ASSERT(rte_ivshmem_metadata_cmdline_generate(buf, 0, METADATA_NAME) < 0,
			"Written command line into small buffer");
	ASSERT(strnlen(buf, sizeof(buf)) == 0, "Buffer is not empty");

	ASSERT(rte_ivshmem_metadata_cmdline_generate(buf, sizeof(buf), NULL) < 0,
			"Written command line for NULL metadata");
	ASSERT(strnlen(buf, sizeof(buf)) == 0, "Buffer is not empty");

	ASSERT(rte_ivshmem_metadata_cmdline_generate(buf, sizeof(buf),
			NONEXISTENT_METADATA) < 0,
			"Writen command line for nonexistent metadata");
	ASSERT(strnlen(buf, sizeof(buf)) == 0, "Buffer is not empty");

	/* add stuff to config */
	ASSERT(rte_ivshmem_metadata_add_memzone(mz, METADATA_NAME) == 0,
			"Failed to add memzone to valid config");
	ASSERT(rte_ivshmem_metadata_add_ring(r, METADATA_NAME) == 0,
			"Failed to add ring to valid config");
	ASSERT(rte_ivshmem_metadata_add_mempool(mp, METADATA_NAME) == 0,
			"Failed to add mempool to valid config");

	/* create config */
	ASSERT(rte_ivshmem_metadata_cmdline_generate(buf, sizeof(buf),
			METADATA_NAME) == 0, "Failed to write command-line");

	/* check if something was written */
	ASSERT(strnlen(buf, sizeof(buf)) != 0, "Buffer is empty");

	/* make sure we don't segfault */
	rte_ivshmem_metadata_dump(stdout, NULL);

	/* dump our metadata */
	rte_ivshmem_metadata_dump(stdout, METADATA_NAME);

	return 0;
}

static int
test_ivshmem_create_duplicate_metadata(void)
{
	ASSERT(rte_ivshmem_metadata_create(DUPLICATE_METADATA) < 0,
			"Creating duplicate metadata should have failed");

	return 0;
}

static int
test_ivshmem_create_metadata_config(void)
{
	struct rte_ivshmem_metadata *metadata;

	rte_ivshmem_metadata_create(METADATA_NAME);

	metadata = mmap_metadata(METADATA_NAME);

	ASSERT(metadata != MAP_FAILED, "Metadata mmaping failed");

	ASSERT(metadata->magic_number == IVSHMEM_MAGIC,
			"Magic number is not that magic");

	ASSERT(strncmp(metadata->name, METADATA_NAME, sizeof(metadata->name)) == 0,
			"Name has not been set up");

	ASSERT(metadata->entry[0].offset == 0, "Offest is not initialized");
	ASSERT(metadata->entry[0].mz.addr == 0, "mz.addr is not initialized");
	ASSERT(metadata->entry[0].mz.len == 0, "mz.len is not initialized");

	return 0;
}

static int
test_ivshmem_create_multiple_metadata_configs(void)
{
	int i;
	char name[IVSHMEM_NAME_LEN];
	struct rte_ivshmem_metadata *metadata;

	for (i = 0; i < RTE_LIBRTE_IVSHMEM_MAX_METADATA_FILES / 2; i++) {
		snprintf(name, sizeof(name), "test_%d", i);
		rte_ivshmem_metadata_create(name);
		metadata = mmap_metadata(name);

		ASSERT(metadata->magic_number == IVSHMEM_MAGIC,
				"Magic number is not that magic");

		ASSERT(strncmp(metadata->name, name, sizeof(metadata->name)) == 0,
				"Name has not been set up");
	}

	return 0;
}

static int
test_ivshmem_create_too_many_metadata_configs(void)
{
	int i;
	char name[IVSHMEM_NAME_LEN];

	for (i = 0; i < RTE_LIBRTE_IVSHMEM_MAX_METADATA_FILES; i++) {
		snprintf(name, sizeof(name), "test_%d", i);
		ASSERT(rte_ivshmem_metadata_create(name) == 0,
				"Create config file failed");
	}

	ASSERT(rte_ivshmem_metadata_create(name) < 0,
			"Create config file didn't fail");

	return 0;
}

enum rte_ivshmem_tests {
	_test_ivshmem_api_test = 0,
	_test_ivshmem_create_metadata_config,
	_test_ivshmem_create_multiple_metadata_configs,
	_test_ivshmem_create_too_many_metadata_configs,
	_test_ivshmem_create_duplicate_metadata,
	_test_ivshmem_create_lots_of_memzones,
	_test_ivshmem_create_duplicate_memzone,
	_last_test,
};

#define RTE_IVSHMEM_TEST_ID "RTE_IVSHMEM_TEST_ID"

static int
launch_all_tests_on_secondary_processes(void)
{
	int ret = 0;
	char id;
	char testid;
	char tmp[PATH_MAX] = {0};
	char prefix[PATH_MAX] = {0};

	get_current_prefix(tmp, sizeof(tmp));

	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);

	const char *argv[] = { prgname, "-c", "1", "-n", "3",
			"--proc-type=secondary", prefix };

	for (id = 0; id < _last_test; id++) {
		testid = (char)(FIRST_TEST + id);
		setenv(RTE_IVSHMEM_TEST_ID, &testid, 1);
		if (launch_proc(argv) != 0)
			return -1;
	}
	return ret;
}

int
test_ivshmem(void)
{
	int testid;

	/* We want to have a clean execution for every test without exposing
	 * private global data structures in rte_ivshmem so we launch each test
	 * on a different secondary process. */
	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {

		/* first, create metadata */
		ASSERT(create_duplicate() == 0, "Creating metadata failed");

		return launch_all_tests_on_secondary_processes();
	}

	testid = *(getenv(RTE_IVSHMEM_TEST_ID)) - FIRST_TEST;

	printf("Secondary process running test %d \n", testid);

	switch (testid) {
	case _test_ivshmem_api_test:
		return test_ivshmem_api_test();

	case _test_ivshmem_create_metadata_config:
		return test_ivshmem_create_metadata_config();

	case _test_ivshmem_create_multiple_metadata_configs:
		return test_ivshmem_create_multiple_metadata_configs();

	case _test_ivshmem_create_too_many_metadata_configs:
		return test_ivshmem_create_too_many_metadata_configs();

	case _test_ivshmem_create_duplicate_metadata:
		return test_ivshmem_create_duplicate_metadata();

	case _test_ivshmem_create_lots_of_memzones:
		return test_ivshmem_create_lots_of_memzones();

	case _test_ivshmem_create_duplicate_memzone:
		return test_ivshmem_create_duplicate_memzone();

	default:
		break;
	}

	return -1;
}

REGISTER_TEST_COMMAND(ivshmem_autotest, test_ivshmem);