xref: /dpdk/app/test/test_net_ip6.c (revision 189fdd3762758486aec347ebdeb9f5bfe74b5600)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2024 Robin Jarry
3  */
4 
5 #include <rte_ip6.h>
6 
7 #include "test.h"
8 
9 static const struct rte_ipv6_addr mask_full = RTE_IPV6_MASK_FULL;
10 static const struct rte_ipv6_addr zero_addr = RTE_IPV6_ADDR_UNSPEC;
11 
12 static int
13 test_ipv6_check_version(void)
14 {
15 	struct rte_ipv6_hdr h;
16 
17 	h.vtc_flow = 0;
18 	TEST_ASSERT_EQUAL(rte_ipv6_check_version(&h), -EINVAL, "");
19 	h.vtc_flow = RTE_BE32(0x7f00ba44);
20 	TEST_ASSERT_EQUAL(rte_ipv6_check_version(&h), -EINVAL, "");
21 	h.vtc_flow = RTE_BE32(0x6badcaca);
22 	TEST_ASSERT_EQUAL(rte_ipv6_check_version(&h), 0, "");
23 
24 	return 0;
25 }
26 
27 static int
28 test_ipv6_addr_mask(void)
29 {
30 	const struct rte_ipv6_addr masked_3 = RTE_IPV6(0xe000, 0, 0, 0, 0, 0, 0, 0);
31 	const struct rte_ipv6_addr masked_42 = RTE_IPV6(0xffff, 0xffff, 0xffc0, 0, 0, 0, 0, 0);
32 	const struct rte_ipv6_addr masked_85 =
33 		RTE_IPV6(0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xf800, 0, 0);
34 	const struct rte_ipv6_addr masked_127 =
35 		RTE_IPV6(0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xfffe);
36 	struct rte_ipv6_addr ip;
37 
38 	ip = mask_full;
39 	rte_ipv6_addr_mask(&ip, 0);
40 	TEST_ASSERT(rte_ipv6_addr_eq(&ip, &zero_addr), "");
41 	TEST_ASSERT_EQUAL(rte_ipv6_mask_depth(&zero_addr), 0, "");
42 
43 	ip = mask_full;
44 	rte_ipv6_addr_mask(&ip, 3);
45 	TEST_ASSERT(rte_ipv6_addr_eq(&ip, &masked_3), "");
46 	TEST_ASSERT_EQUAL(rte_ipv6_mask_depth(&masked_3), 3, "");
47 
48 	ip = mask_full;
49 	rte_ipv6_addr_mask(&ip, 42);
50 	TEST_ASSERT(rte_ipv6_addr_eq(&ip, &masked_42), "");
51 	TEST_ASSERT_EQUAL(rte_ipv6_mask_depth(&masked_42), 42, "");
52 
53 	ip = mask_full;
54 	rte_ipv6_addr_mask(&ip, 85);
55 	TEST_ASSERT(rte_ipv6_addr_eq(&ip, &masked_85), "");
56 	TEST_ASSERT_EQUAL(rte_ipv6_mask_depth(&masked_85), 85, "");
57 
58 	ip = mask_full;
59 	rte_ipv6_addr_mask(&ip, 127);
60 	TEST_ASSERT(rte_ipv6_addr_eq(&ip, &masked_127), "");
61 	TEST_ASSERT_EQUAL(rte_ipv6_mask_depth(&masked_127), 127, "");
62 
63 	ip = mask_full;
64 	rte_ipv6_addr_mask(&ip, 128);
65 	TEST_ASSERT(rte_ipv6_addr_eq(&ip, &mask_full), "");
66 	TEST_ASSERT_EQUAL(rte_ipv6_mask_depth(&mask_full), 128, "");
67 
68 	const struct rte_ipv6_addr mask_holed =
69 		RTE_IPV6(0xffff, 0xffff, 0xffff, 0xefff, 0xffff, 0xffff, 0xffff, 0xffff);
70 	TEST_ASSERT_EQUAL(rte_ipv6_mask_depth(&mask_holed), 51, "");
71 
72 	return TEST_SUCCESS;
73 }
74 
75 static int
76 test_ipv6_addr_eq_prefix(void)
77 {
78 	const struct rte_ipv6_addr ip1 =
79 		RTE_IPV6(0x2a01, 0xcb00, 0x0254, 0x3300, 0x1b9f, 0x8071, 0x67cd, 0xbf20);
80 	const struct rte_ipv6_addr ip2 =
81 		RTE_IPV6(0x2a01, 0xcb00, 0x0254, 0x3300, 0x6239, 0xe1f4, 0x7a0b, 0x2371);
82 	const struct rte_ipv6_addr ip3 =
83 		RTE_IPV6(0xfd10, 0x0039, 0x0208, 0x0001, 0x0000, 0x0000, 0x0000, 0x1008);
84 
85 	TEST_ASSERT(rte_ipv6_addr_eq_prefix(&ip1, &ip2, 1), "");
86 	TEST_ASSERT(rte_ipv6_addr_eq_prefix(&ip1, &ip2, 37), "");
87 	TEST_ASSERT(rte_ipv6_addr_eq_prefix(&ip1, &ip2, 64), "");
88 	TEST_ASSERT(!rte_ipv6_addr_eq_prefix(&ip1, &ip2, 112), "");
89 	TEST_ASSERT(rte_ipv6_addr_eq_prefix(&ip1, &ip3, 0), "");
90 	TEST_ASSERT(!rte_ipv6_addr_eq_prefix(&ip1, &ip3, 13), "");
91 
92 	return TEST_SUCCESS;
93 }
94 
95 static int
96 test_ipv6_addr_kind(void)
97 {
98 	TEST_ASSERT(rte_ipv6_addr_is_unspec(&zero_addr), "");
99 	TEST_ASSERT(!rte_ipv6_addr_is_linklocal(&zero_addr), "");
100 	TEST_ASSERT(!rte_ipv6_addr_is_loopback(&zero_addr), "");
101 	TEST_ASSERT(!rte_ipv6_addr_is_mcast(&zero_addr), "");
102 
103 	const struct rte_ipv6_addr ucast =
104 		RTE_IPV6(0x2a01, 0xcb00, 0x0254, 0x3300, 0x6239, 0xe1f4, 0x7a0b, 0x2371);
105 	TEST_ASSERT(!rte_ipv6_addr_is_unspec(&ucast), "");
106 	TEST_ASSERT(!rte_ipv6_addr_is_linklocal(&ucast), "");
107 	TEST_ASSERT(!rte_ipv6_addr_is_loopback(&ucast), "");
108 	TEST_ASSERT(!rte_ipv6_addr_is_mcast(&ucast), "");
109 
110 	const struct rte_ipv6_addr mcast = RTE_IPV6(0xff01, 0, 0, 0, 0, 0, 0, 1);
111 	TEST_ASSERT(!rte_ipv6_addr_is_unspec(&mcast), "");
112 	TEST_ASSERT(!rte_ipv6_addr_is_linklocal(&mcast), "");
113 	TEST_ASSERT(!rte_ipv6_addr_is_loopback(&mcast), "");
114 	TEST_ASSERT(rte_ipv6_addr_is_mcast(&mcast), "");
115 
116 	const struct rte_ipv6_addr lo = RTE_IPV6_ADDR_LOOPBACK;
117 	TEST_ASSERT(!rte_ipv6_addr_is_unspec(&lo), "");
118 	TEST_ASSERT(!rte_ipv6_addr_is_linklocal(&lo), "");
119 	TEST_ASSERT(rte_ipv6_addr_is_loopback(&lo), "");
120 	TEST_ASSERT(!rte_ipv6_addr_is_mcast(&lo), "");
121 
122 	const struct rte_ipv6_addr local =
123 		RTE_IPV6(0xfe80, 0, 0, 0, 0x5a84, 0xc52c, 0x6aef, 0x4639);
124 	TEST_ASSERT(!rte_ipv6_addr_is_unspec(&local), "");
125 	TEST_ASSERT(rte_ipv6_addr_is_linklocal(&local), "");
126 	TEST_ASSERT(!rte_ipv6_addr_is_loopback(&local), "");
127 	TEST_ASSERT(!rte_ipv6_addr_is_mcast(&local), "");
128 
129 	return TEST_SUCCESS;
130 }
131 
132 static int
133 test_ipv6_llocal_from_ethernet(void)
134 {
135 	const struct rte_ether_addr local_mac = {{0x04, 0x7b, 0xcb, 0x5c, 0x08, 0x44}};
136 	const struct rte_ipv6_addr local_ip =
137 		RTE_IPV6(0xfe80, 0, 0, 0, 0x047b, 0xcbff, 0xfe5c, 0x0844);
138 	struct rte_ipv6_addr ip;
139 
140 	rte_ipv6_llocal_from_ethernet(&ip, &local_mac);
141 	TEST_ASSERT(rte_ipv6_addr_eq(&ip, &local_ip), "");
142 
143 	return TEST_SUCCESS;
144 }
145 
146 static int
147 test_ipv6_solnode_from_addr(void)
148 {
149 	struct rte_ipv6_addr sol;
150 
151 	const struct rte_ipv6_addr llocal =
152 		RTE_IPV6(0xfe80, 0, 0, 0, 0x047b, 0xcbff, 0xfe5c, 0x0844);
153 	const struct rte_ipv6_addr llocal_sol =
154 		RTE_IPV6(0xff02, 0, 0, 0, 0, 0x0001, 0xff5c, 0x0844);
155 	rte_ipv6_solnode_from_addr(&sol, &llocal);
156 	TEST_ASSERT(rte_ipv6_addr_eq(&sol, &llocal_sol), "");
157 
158 	const struct rte_ipv6_addr ucast =
159 		RTE_IPV6(0x2a01, 0xcb00, 0x0254, 0x3300, 0x1b9f, 0x8071, 0x67cd, 0xbf20);
160 	const struct rte_ipv6_addr ucast_sol =
161 		RTE_IPV6(0xff02, 0, 0, 0, 0, 0x0001, 0xffcd, 0xbf20);
162 	rte_ipv6_solnode_from_addr(&sol, &ucast);
163 	TEST_ASSERT(rte_ipv6_addr_eq(&sol, &ucast_sol), "");
164 
165 	return TEST_SUCCESS;
166 }
167 
168 static int
169 test_ether_mcast_from_ipv6(void)
170 {
171 	const struct rte_ether_addr mcast_mac = {{0x33, 0x33, 0xd3, 0x00, 0x02, 0x01}};
172 	const struct rte_ipv6_addr mcast_ip =
173 		RTE_IPV6(0xff02, 0, 0, 0x0201, 0, 0, 0xd300, 0x0201);
174 	struct rte_ether_addr mac;
175 
176 	rte_ether_mcast_from_ipv6(&mac, &mcast_ip);
177 	TEST_ASSERT(rte_is_same_ether_addr(&mac, &mcast_mac), "");
178 
179 	return TEST_SUCCESS;
180 }
181 
182 static int
183 test_net_ipv6(void)
184 {
185 	TEST_ASSERT_SUCCESS(test_ipv6_check_version(), "");
186 	TEST_ASSERT_SUCCESS(test_ipv6_addr_mask(), "");
187 	TEST_ASSERT_SUCCESS(test_ipv6_addr_eq_prefix(), "");
188 	TEST_ASSERT_SUCCESS(test_ipv6_addr_kind(), "");
189 	TEST_ASSERT_SUCCESS(test_ipv6_llocal_from_ethernet(), "");
190 	TEST_ASSERT_SUCCESS(test_ipv6_solnode_from_addr(), "");
191 	TEST_ASSERT_SUCCESS(test_ether_mcast_from_ipv6(), "");
192 	return TEST_SUCCESS;
193 }
194 
195 REGISTER_FAST_TEST(net_ipv6_autotest, true, true, test_net_ipv6);
196