File: test_net_ether.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 (165 lines) | stat: -rw-r--r-- 3,994 bytes parent folder | download | duplicates (2)
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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright (c) 2023 Stephen Hemminger
 */

#include <rte_ether.h>

#include <rte_test.h>
#include "test.h"

#define N 1000000

static const struct rte_ether_addr zero_ea;
static const struct rte_ether_addr bcast_ea = {
	.addr_bytes = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
};

static int
test_ether_addr(void)
{
	struct rte_ether_addr rand_ea = { };
	unsigned int i;

	RTE_TEST_ASSERT(rte_is_zero_ether_addr(&zero_ea), "Zero address is not zero");
	RTE_TEST_ASSERT(!rte_is_zero_ether_addr(&bcast_ea), "Broadcast is zero");

	for (i = 0; i < N; i++) {
		rte_eth_random_addr(rand_ea.addr_bytes);
		RTE_TEST_ASSERT(!rte_is_zero_ether_addr(&rand_ea),
				"Random address is zero");
		RTE_TEST_ASSERT(rte_is_unicast_ether_addr(&rand_ea),
				"Random address is not unicast");
		RTE_TEST_ASSERT(rte_is_local_admin_ether_addr(&rand_ea),
				"Random address is not local admin");
	}

	return 0;
}

static int
test_format_addr(void)
{
	struct rte_ether_addr rand_ea = { };
	char buf[RTE_ETHER_ADDR_FMT_SIZE];
	unsigned int i;

	for (i = 0; i < N; i++) {
		struct rte_ether_addr result = { };
		int ret;

		rte_eth_random_addr(rand_ea.addr_bytes);

		rte_ether_format_addr(buf, sizeof(buf), &rand_ea);

		ret = rte_ether_unformat_addr(buf, &result);
		if (ret != 0) {
			fprintf(stderr, "rte_ether_unformat_addr(%s) failed\n", buf);
			return -1;
		}
		RTE_TEST_ASSERT(rte_is_same_ether_addr(&rand_ea, &result),
			"rte_ether_format/unformat mismatch");
	}
	return 0;

}

static int
test_unformat_addr(void)
{
	const struct rte_ether_addr expected = {
		.addr_bytes = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc },
	};
	const struct rte_ether_addr nozero_ea = {
		.addr_bytes = { 1, 2, 3, 4, 5, 6 },
	};
	struct rte_ether_addr result;
	int ret;

	/* Test IETF format */
	memset(&result, 0, sizeof(result));
	ret = rte_ether_unformat_addr("12:34:56:78:9a:bc", &result);
	RTE_TEST_ASSERT(ret == 0, "IETF unformat failed");
	RTE_TEST_ASSERT(rte_is_same_ether_addr(&expected, &result),
		"IETF unformat mismatch");

	/* Test IEEE format */
	memset(&result, 0, sizeof(result));
	ret = rte_ether_unformat_addr("12-34-56-78-9A-BC", &result);
	RTE_TEST_ASSERT(ret == 0, "IEEE unformat failed");
	RTE_TEST_ASSERT(rte_is_same_ether_addr(&expected, &result),
			"IEEE unformat mismatch");

	/* Test Cisco format */
	memset(&result, 0, sizeof(result));
	ret = rte_ether_unformat_addr("1234.5678.9ABC", &result);
	RTE_TEST_ASSERT(ret == 0, "Cisco unformat failed");
	RTE_TEST_ASSERT(rte_is_same_ether_addr(&expected, &result),
			"Cisco unformat mismatch");

	/* Test no leading zeros - IETF */
	memset(&result, 0, sizeof(result));
	ret = rte_ether_unformat_addr("1:2:3:4:5:6", &result);
	RTE_TEST_ASSERT(ret == 0, "IETF leading zero failed");
	RTE_TEST_ASSERT(rte_is_same_ether_addr(&nozero_ea, &result),
			"IETF leading zero mismatch");

	/* Test no-leading zero - IEEE format */
	memset(&result, 0, sizeof(result));
	ret = rte_ether_unformat_addr("1-2-3-4-5-6", &result);
	RTE_TEST_ASSERT(ret == 0, "IEEE leading zero failed");
	RTE_TEST_ASSERT(rte_is_same_ether_addr(&nozero_ea, &result),
			"IEEE leading zero mismatch");


	return 0;
}

static int
test_invalid_addr(void)
{
	static const char * const invalid[] = {
		"123",
		"123:456",
		"12:34:56:78:9a:gh",
		"12:34:56:78:9a",
		"100:34:56:78:9a:bc",
		"34-56-78-9a-bc",
		"12:34:56-78:9a:bc",
		"12:34:56.78:9a:bc",
		"123:456:789:abc",
		"NOT.AN.ADDRESS",
		"102.304.506",
		"",
	};
	struct rte_ether_addr result;
	unsigned int i;

	for (i = 0; i < RTE_DIM(invalid); ++i) {
		if (!rte_ether_unformat_addr(invalid[i], &result)) {
			fprintf(stderr, "rte_ether_unformat_addr(%s) succeeded!\n",
				invalid[i]);
			return -1;
		}
	}
	return 0;
}

static int
test_net_ether(void)
{
	if (test_ether_addr())
		return -1;

	if (test_format_addr())
		return -1;

	if (test_unformat_addr())
		return -1;

	if (test_invalid_addr())
		return -1;

	return 0;
}

REGISTER_FAST_TEST(net_ether_autotest, true, true, test_net_ether);