Branch data Line data Source code
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 : 1 : test_ipv6_addr_mask(void)
29 : : {
30 : 1 : const struct rte_ipv6_addr masked_3 = RTE_IPV6(0xe000, 0, 0, 0, 0, 0, 0, 0);
31 : 1 : const struct rte_ipv6_addr masked_42 = RTE_IPV6(0xffff, 0xffff, 0xffc0, 0, 0, 0, 0, 0);
32 : 1 : const struct rte_ipv6_addr masked_85 =
33 : : RTE_IPV6(0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xf800, 0, 0);
34 : 1 : 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 : 1 : ip = mask_full;
39 : : rte_ipv6_addr_mask(&ip, 0);
40 [ - + ]: 1 : TEST_ASSERT(rte_ipv6_addr_eq(&ip, &zero_addr), "");
41 : : TEST_ASSERT_EQUAL(rte_ipv6_mask_depth(&zero_addr), 0, "");
42 : :
43 : 1 : ip = mask_full;
44 : : rte_ipv6_addr_mask(&ip, 3);
45 [ - + ]: 1 : TEST_ASSERT(rte_ipv6_addr_eq(&ip, &masked_3), "");
46 [ - + ]: 1 : TEST_ASSERT_EQUAL(rte_ipv6_mask_depth(&masked_3), 3, "");
47 : :
48 : 1 : ip = mask_full;
49 : : rte_ipv6_addr_mask(&ip, 42);
50 [ - + ]: 1 : TEST_ASSERT(rte_ipv6_addr_eq(&ip, &masked_42), "");
51 [ - + ]: 1 : TEST_ASSERT_EQUAL(rte_ipv6_mask_depth(&masked_42), 42, "");
52 : :
53 : 1 : ip = mask_full;
54 : : rte_ipv6_addr_mask(&ip, 85);
55 [ - + ]: 1 : TEST_ASSERT(rte_ipv6_addr_eq(&ip, &masked_85), "");
56 [ - + ]: 1 : TEST_ASSERT_EQUAL(rte_ipv6_mask_depth(&masked_85), 85, "");
57 : :
58 [ - + ]: 1 : ip = mask_full;
59 : : rte_ipv6_addr_mask(&ip, 127);
60 [ - + ]: 1 : TEST_ASSERT(rte_ipv6_addr_eq(&ip, &masked_127), "");
61 [ - + ]: 1 : TEST_ASSERT_EQUAL(rte_ipv6_mask_depth(&masked_127), 127, "");
62 : :
63 [ - + ]: 1 : ip = mask_full;
64 : : rte_ipv6_addr_mask(&ip, 128);
65 [ - + ]: 1 : TEST_ASSERT(rte_ipv6_addr_eq(&ip, &mask_full), "");
66 [ - + ]: 1 : TEST_ASSERT_EQUAL(rte_ipv6_mask_depth(&mask_full), 128, "");
67 : :
68 : 1 : const struct rte_ipv6_addr mask_holed =
69 : : RTE_IPV6(0xffff, 0xffff, 0xffff, 0xefff, 0xffff, 0xffff, 0xffff, 0xffff);
70 [ - + ]: 1 : TEST_ASSERT_EQUAL(rte_ipv6_mask_depth(&mask_holed), 51, "");
71 : :
72 : : return TEST_SUCCESS;
73 : : }
74 : :
75 : : static int
76 : 1 : test_ipv6_addr_eq_prefix(void)
77 : : {
78 : 1 : const struct rte_ipv6_addr ip1 =
79 : : RTE_IPV6(0x2a01, 0xcb00, 0x0254, 0x3300, 0x1b9f, 0x8071, 0x67cd, 0xbf20);
80 : 1 : const struct rte_ipv6_addr ip2 =
81 : : RTE_IPV6(0x2a01, 0xcb00, 0x0254, 0x3300, 0x6239, 0xe1f4, 0x7a0b, 0x2371);
82 : 1 : const struct rte_ipv6_addr ip3 =
83 : : RTE_IPV6(0xfd10, 0x0039, 0x0208, 0x0001, 0x0000, 0x0000, 0x0000, 0x1008);
84 : :
85 [ - + ]: 1 : TEST_ASSERT(rte_ipv6_addr_eq_prefix(&ip1, &ip2, 1), "");
86 [ - + ]: 1 : TEST_ASSERT(rte_ipv6_addr_eq_prefix(&ip1, &ip2, 37), "");
87 [ - + ]: 1 : TEST_ASSERT(rte_ipv6_addr_eq_prefix(&ip1, &ip2, 64), "");
88 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_eq_prefix(&ip1, &ip2, 112), "");
89 [ - + ]: 1 : TEST_ASSERT(rte_ipv6_addr_eq_prefix(&ip1, &ip3, 0), "");
90 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_eq_prefix(&ip1, &ip3, 13), "");
91 : :
92 : : return TEST_SUCCESS;
93 : : }
94 : :
95 : : static int
96 [ - + ]: 1 : test_ipv6_addr_kind(void)
97 : : {
98 [ - + ]: 1 : TEST_ASSERT(rte_ipv6_addr_is_unspec(&zero_addr), "");
99 : : TEST_ASSERT(!rte_ipv6_addr_is_linklocal(&zero_addr), "");
100 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_loopback(&zero_addr), "");
101 : : TEST_ASSERT(!rte_ipv6_addr_is_mcast(&zero_addr), "");
102 [ - + ]: 1 : TEST_ASSERT(rte_ipv6_addr_is_v4compat(&zero_addr), "");
103 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_v4mapped(&zero_addr), "");
104 : :
105 [ - + ]: 1 : const struct rte_ipv6_addr ucast =
106 : : RTE_IPV6(0x2a01, 0xcb00, 0x0254, 0x3300, 0x6239, 0xe1f4, 0x7a0b, 0x2371);
107 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_unspec(&ucast), "");
108 : : TEST_ASSERT(!rte_ipv6_addr_is_linklocal(&ucast), "");
109 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_loopback(&ucast), "");
110 : : TEST_ASSERT(!rte_ipv6_addr_is_mcast(&ucast), "");
111 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_v4compat(&ucast), "");
112 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_v4mapped(&ucast), "");
113 : :
114 [ - + ]: 1 : const struct rte_ipv6_addr mcast = RTE_IPV6(0xff01, 0, 0, 0, 0, 0, 0, 1);
115 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_unspec(&mcast), "");
116 : : TEST_ASSERT(!rte_ipv6_addr_is_linklocal(&mcast), "");
117 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_loopback(&mcast), "");
118 : : TEST_ASSERT(rte_ipv6_addr_is_mcast(&mcast), "");
119 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_v4compat(&mcast), "");
120 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_v4mapped(&mcast), "");
121 : :
122 [ - + ]: 1 : const struct rte_ipv6_addr lo = RTE_IPV6_ADDR_LOOPBACK;
123 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_unspec(&lo), "");
124 : : TEST_ASSERT(!rte_ipv6_addr_is_linklocal(&lo), "");
125 [ - + ]: 1 : TEST_ASSERT(rte_ipv6_addr_is_loopback(&lo), "");
126 : : TEST_ASSERT(!rte_ipv6_addr_is_mcast(&lo), "");
127 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_v4compat(&lo), "");
128 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_v4mapped(&lo), "");
129 : :
130 [ - + ]: 1 : const struct rte_ipv6_addr local =
131 : : RTE_IPV6(0xfe80, 0, 0, 0, 0x5a84, 0xc52c, 0x6aef, 0x4639);
132 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_unspec(&local), "");
133 : : TEST_ASSERT(rte_ipv6_addr_is_linklocal(&local), "");
134 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_loopback(&local), "");
135 : : TEST_ASSERT(!rte_ipv6_addr_is_mcast(&local), "");
136 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_v4compat(&local), "");
137 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_v4mapped(&local), "");
138 : :
139 [ - + ]: 1 : const struct rte_ipv6_addr v4compat = RTE_IPV6(0, 0, 0, 0, 0, 0, 0xc0a8, 0x0001);
140 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_unspec(&v4compat), "");
141 : : TEST_ASSERT(!rte_ipv6_addr_is_linklocal(&v4compat), "");
142 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_loopback(&v4compat), "");
143 : : TEST_ASSERT(!rte_ipv6_addr_is_mcast(&v4compat), "");
144 [ - + ]: 1 : TEST_ASSERT(rte_ipv6_addr_is_v4compat(&v4compat), "");
145 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_v4mapped(&v4compat), "");
146 : :
147 [ - + ]: 1 : const struct rte_ipv6_addr v4mapped = RTE_IPV6(0, 0, 0, 0, 0, 0xffff, 0xc0a8, 0x0001);
148 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_unspec(&v4mapped), "");
149 : : TEST_ASSERT(!rte_ipv6_addr_is_linklocal(&v4mapped), "");
150 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_loopback(&v4mapped), "");
151 : : TEST_ASSERT(!rte_ipv6_addr_is_mcast(&v4mapped), "");
152 [ - + ]: 1 : TEST_ASSERT(!rte_ipv6_addr_is_v4compat(&v4mapped), "");
153 [ - + ]: 1 : TEST_ASSERT(rte_ipv6_addr_is_v4mapped(&v4mapped), "");
154 : :
155 : : return TEST_SUCCESS;
156 : : }
157 : :
158 : : static int
159 : 1 : test_ipv6_llocal_from_ethernet(void)
160 : : {
161 : 1 : const struct rte_ether_addr local_mac = {{0x04, 0x7b, 0xcb, 0x5c, 0x08, 0x44}};
162 : 1 : const struct rte_ipv6_addr local_ip =
163 : : RTE_IPV6(0xfe80, 0, 0, 0, 0x047b, 0xcbff, 0xfe5c, 0x0844);
164 : : struct rte_ipv6_addr ip;
165 : :
166 : 1 : rte_ipv6_llocal_from_ethernet(&ip, &local_mac);
167 [ - + ]: 1 : TEST_ASSERT(rte_ipv6_addr_eq(&ip, &local_ip), "");
168 : :
169 : : return TEST_SUCCESS;
170 : : }
171 : :
172 : : static int
173 : 1 : test_ipv6_solnode_from_addr(void)
174 : : {
175 : : struct rte_ipv6_addr sol;
176 : :
177 : : const struct rte_ipv6_addr llocal =
178 : : RTE_IPV6(0xfe80, 0, 0, 0, 0x047b, 0xcbff, 0xfe5c, 0x0844);
179 [ - + ]: 1 : const struct rte_ipv6_addr llocal_sol =
180 : : RTE_IPV6(0xff02, 0, 0, 0, 0, 0x0001, 0xff5c, 0x0844);
181 : : rte_ipv6_solnode_from_addr(&sol, &llocal);
182 [ - + ]: 1 : TEST_ASSERT(rte_ipv6_addr_eq(&sol, &llocal_sol), "");
183 : :
184 : : const struct rte_ipv6_addr ucast =
185 : : RTE_IPV6(0x2a01, 0xcb00, 0x0254, 0x3300, 0x1b9f, 0x8071, 0x67cd, 0xbf20);
186 [ - + ]: 1 : const struct rte_ipv6_addr ucast_sol =
187 : : RTE_IPV6(0xff02, 0, 0, 0, 0, 0x0001, 0xffcd, 0xbf20);
188 : : rte_ipv6_solnode_from_addr(&sol, &ucast);
189 [ - + ]: 1 : TEST_ASSERT(rte_ipv6_addr_eq(&sol, &ucast_sol), "");
190 : :
191 : : return TEST_SUCCESS;
192 : : }
193 : :
194 : : static int
195 : : test_ether_mcast_from_ipv6(void)
196 : : {
197 : : const struct rte_ether_addr mcast_mac = {{0x33, 0x33, 0xd3, 0x00, 0x02, 0x01}};
198 : : const struct rte_ipv6_addr mcast_ip =
199 : : RTE_IPV6(0xff02, 0, 0, 0x0201, 0, 0, 0xd300, 0x0201);
200 : : struct rte_ether_addr mac;
201 : :
202 : : rte_ether_mcast_from_ipv6(&mac, &mcast_ip);
203 : : TEST_ASSERT(rte_is_same_ether_addr(&mac, &mcast_mac), "");
204 : :
205 : : return TEST_SUCCESS;
206 : : }
207 : :
208 : : static int
209 : 1 : test_net_ipv6(void)
210 : : {
211 : : TEST_ASSERT_SUCCESS(test_ipv6_check_version(), "");
212 [ - + ]: 1 : TEST_ASSERT_SUCCESS(test_ipv6_addr_mask(), "");
213 [ - + ]: 1 : TEST_ASSERT_SUCCESS(test_ipv6_addr_eq_prefix(), "");
214 [ - + ]: 1 : TEST_ASSERT_SUCCESS(test_ipv6_addr_kind(), "");
215 [ - + ]: 1 : TEST_ASSERT_SUCCESS(test_ipv6_llocal_from_ethernet(), "");
216 [ - + ]: 1 : TEST_ASSERT_SUCCESS(test_ipv6_solnode_from_addr(), "");
217 : : TEST_ASSERT_SUCCESS(test_ether_mcast_from_ipv6(), "");
218 : : return TEST_SUCCESS;
219 : : }
220 : :
221 : 254 : REGISTER_FAST_TEST(net_ipv6_autotest, true, true, test_net_ipv6);
|