|
1 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later
|
2 | 2 |
|
3 | 3 | #include <kunit/test.h>
|
| 4 | + |
| 5 | +/* GSO */ |
| 6 | + |
4 | 7 | #include <linux/skbuff.h>
|
5 | 8 |
|
6 | 9 | static const char hdr[] = "abcdefgh";
|
@@ -258,17 +261,130 @@ static void gso_test_func(struct kunit *test)
|
258 | 261 | consume_skb(skb);
|
259 | 262 | }
|
260 | 263 |
|
261 |
| -static struct kunit_case gso_test_cases[] = { |
262 |
| - KUNIT_CASE_PARAM(gso_test_func, gso_test_gen_params), |
263 |
| - {} |
| 264 | +/* IP tunnel flags */ |
| 265 | + |
| 266 | +#include <net/ip_tunnels.h> |
| 267 | + |
| 268 | +struct ip_tunnel_flags_test { |
| 269 | + const char *name; |
| 270 | + |
| 271 | + const u16 *src_bits; |
| 272 | + const u16 *exp_bits; |
| 273 | + u8 src_num; |
| 274 | + u8 exp_num; |
| 275 | + |
| 276 | + __be16 exp_val; |
| 277 | + bool exp_comp; |
| 278 | +}; |
| 279 | + |
| 280 | +#define IP_TUNNEL_FLAGS_TEST(n, src, comp, eval, exp) { \ |
| 281 | + .name = (n), \ |
| 282 | + .src_bits = (src), \ |
| 283 | + .src_num = ARRAY_SIZE(src), \ |
| 284 | + .exp_comp = (comp), \ |
| 285 | + .exp_val = (eval), \ |
| 286 | + .exp_bits = (exp), \ |
| 287 | + .exp_num = ARRAY_SIZE(exp), \ |
| 288 | +} |
| 289 | + |
| 290 | +/* These are __be16-compatible and can be compared as is */ |
| 291 | +static const u16 ip_tunnel_flags_1[] = { |
| 292 | + IP_TUNNEL_KEY_BIT, |
| 293 | + IP_TUNNEL_STRICT_BIT, |
| 294 | + IP_TUNNEL_ERSPAN_OPT_BIT, |
| 295 | +}; |
| 296 | + |
| 297 | +/* Due to the previous flags design limitation, setting either |
| 298 | + * ``IP_TUNNEL_CSUM_BIT`` (on Big Endian) or ``IP_TUNNEL_DONT_FRAGMENT_BIT`` |
| 299 | + * (on Little) also sets VTI/ISATAP bit. In the bitmap implementation, they |
| 300 | + * correspond to ``BIT(16)``, which is bigger than ``U16_MAX``, but still is |
| 301 | + * backward-compatible. |
| 302 | + */ |
| 303 | +#ifdef __LITTLE_ENDIAN |
| 304 | +#define IP_TUNNEL_CONFLICT_BIT IP_TUNNEL_DONT_FRAGMENT_BIT |
| 305 | +#else |
| 306 | +#define IP_TUNNEL_CONFLICT_BIT IP_TUNNEL_CSUM_BIT |
| 307 | +#endif |
| 308 | + |
| 309 | +static const u16 ip_tunnel_flags_2_src[] = { |
| 310 | + IP_TUNNEL_CONFLICT_BIT, |
| 311 | +}; |
| 312 | + |
| 313 | +static const u16 ip_tunnel_flags_2_exp[] = { |
| 314 | + IP_TUNNEL_CONFLICT_BIT, |
| 315 | + IP_TUNNEL_SIT_ISATAP_BIT, |
264 | 316 | };
|
265 | 317 |
|
266 |
| -static struct kunit_suite gso_test_suite = { |
267 |
| - .name = "net_core_gso", |
268 |
| - .test_cases = gso_test_cases, |
| 318 | +/* Bits 17 and higher are not compatible with __be16 flags */ |
| 319 | +static const u16 ip_tunnel_flags_3_src[] = { |
| 320 | + IP_TUNNEL_VXLAN_OPT_BIT, |
| 321 | + 17, |
| 322 | + 18, |
| 323 | + 20, |
269 | 324 | };
|
270 | 325 |
|
271 |
| -kunit_test_suite(gso_test_suite); |
| 326 | +static const u16 ip_tunnel_flags_3_exp[] = { |
| 327 | + IP_TUNNEL_VXLAN_OPT_BIT, |
| 328 | +}; |
| 329 | + |
| 330 | +static const struct ip_tunnel_flags_test ip_tunnel_flags_test[] = { |
| 331 | + IP_TUNNEL_FLAGS_TEST("compat", ip_tunnel_flags_1, true, |
| 332 | + cpu_to_be16(BIT(IP_TUNNEL_KEY_BIT) | |
| 333 | + BIT(IP_TUNNEL_STRICT_BIT) | |
| 334 | + BIT(IP_TUNNEL_ERSPAN_OPT_BIT)), |
| 335 | + ip_tunnel_flags_1), |
| 336 | + IP_TUNNEL_FLAGS_TEST("conflict", ip_tunnel_flags_2_src, true, |
| 337 | + VTI_ISVTI, ip_tunnel_flags_2_exp), |
| 338 | + IP_TUNNEL_FLAGS_TEST("new", ip_tunnel_flags_3_src, |
| 339 | + /* This must be set to ``false`` once |
| 340 | + * ``__IP_TUNNEL_FLAG_NUM`` goes above 17. |
| 341 | + */ |
| 342 | + true, cpu_to_be16(BIT(IP_TUNNEL_VXLAN_OPT_BIT)), |
| 343 | + ip_tunnel_flags_3_exp), |
| 344 | +}; |
| 345 | + |
| 346 | +static void |
| 347 | +ip_tunnel_flags_test_case_to_desc(const struct ip_tunnel_flags_test *t, |
| 348 | + char *desc) |
| 349 | +{ |
| 350 | + strscpy(desc, t->name, KUNIT_PARAM_DESC_SIZE); |
| 351 | +} |
| 352 | +KUNIT_ARRAY_PARAM(ip_tunnel_flags_test, ip_tunnel_flags_test, |
| 353 | + ip_tunnel_flags_test_case_to_desc); |
| 354 | + |
| 355 | +static void ip_tunnel_flags_test_run(struct kunit *test) |
| 356 | +{ |
| 357 | + const struct ip_tunnel_flags_test *t = test->param_value; |
| 358 | + IP_TUNNEL_DECLARE_FLAGS(src) = { }; |
| 359 | + IP_TUNNEL_DECLARE_FLAGS(exp) = { }; |
| 360 | + IP_TUNNEL_DECLARE_FLAGS(out); |
| 361 | + |
| 362 | + for (u32 j = 0; j < t->src_num; j++) |
| 363 | + __set_bit(t->src_bits[j], src); |
| 364 | + for (u32 j = 0; j < t->exp_num; j++) |
| 365 | + __set_bit(t->exp_bits[j], exp); |
| 366 | + |
| 367 | + KUNIT_ASSERT_EQ(test, t->exp_comp, |
| 368 | + ip_tunnel_flags_is_be16_compat(src)); |
| 369 | + KUNIT_ASSERT_EQ(test, (__force u16)t->exp_val, |
| 370 | + (__force u16)ip_tunnel_flags_to_be16(src)); |
| 371 | + |
| 372 | + ip_tunnel_flags_from_be16(out, t->exp_val); |
| 373 | + KUNIT_ASSERT_TRUE(test, __ipt_flag_op(bitmap_equal, exp, out)); |
| 374 | +} |
| 375 | + |
| 376 | +static struct kunit_case net_test_cases[] = { |
| 377 | + KUNIT_CASE_PARAM(gso_test_func, gso_test_gen_params), |
| 378 | + KUNIT_CASE_PARAM(ip_tunnel_flags_test_run, |
| 379 | + ip_tunnel_flags_test_gen_params), |
| 380 | + { }, |
| 381 | +}; |
| 382 | + |
| 383 | +static struct kunit_suite net_test_suite = { |
| 384 | + .name = "net_core", |
| 385 | + .test_cases = net_test_cases, |
| 386 | +}; |
| 387 | +kunit_test_suite(net_test_suite); |
272 | 388 |
|
| 389 | +MODULE_DESCRIPTION("KUnit tests for networking core"); |
273 | 390 | MODULE_LICENSE("GPL");
|
274 |
| -MODULE_DESCRIPTION("KUnit tests for segmentation offload"); |
|
0 commit comments