Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf, test_run: Propagate bpf_flow_dissect's retval to user's bpf_attr.test.retval

Formerly, a boolean denoting whether bpf_flow_dissect returned BPF_OK
was set into 'bpf_attr.test.retval'.

Augment this, so users can check the actual return code of the dissector
program under test.

Existing prog_tests/flow_dissector*.c tests were correspondingly changed
to check against each test's expected retval.

Also, tests' resulting 'flow_keys' are verified only in case the expected
retval is BPF_OK. This allows adding new tests that expect non BPF_OK.

Signed-off-by: Shmulik Ladkani <shmulik.ladkani@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Stanislav Fomichev <sdf@google.com>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20220821113519.116765-4-shmulik.ladkani@gmail.com

authored by

Shmulik Ladkani and committed by
Daniel Borkmann
5deedfbe 91350fe1

+24 -3
+1 -1
net/bpf/test_run.c
··· 1445 1445 bpf_test_timer_enter(&t); 1446 1446 do { 1447 1447 retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN, 1448 - size, flags) == BPF_OK; 1448 + size, flags); 1449 1449 } while (bpf_test_timer_continue(&t, 1, repeat, &ret, &duration)); 1450 1450 bpf_test_timer_leave(&t); 1451 1451
+22 -1
tools/testing/selftests/bpf/prog_tests/flow_dissector.c
··· 100 100 } pkt; 101 101 struct bpf_flow_keys keys; 102 102 __u32 flags; 103 + __u32 retval; 103 104 }; 104 105 105 106 #define VLAN_HLEN 4 ··· 127 126 .sport = 80, 128 127 .dport = 8080, 129 128 }, 129 + .retval = BPF_OK, 130 130 }, 131 131 { 132 132 .name = "ipv6", ··· 148 146 .sport = 80, 149 147 .dport = 8080, 150 148 }, 149 + .retval = BPF_OK, 151 150 }, 152 151 { 153 152 .name = "802.1q-ipv4", ··· 171 168 .sport = 80, 172 169 .dport = 8080, 173 170 }, 171 + .retval = BPF_OK, 174 172 }, 175 173 { 176 174 .name = "802.1ad-ipv6", ··· 195 191 .sport = 80, 196 192 .dport = 8080, 197 193 }, 194 + .retval = BPF_OK, 198 195 }, 199 196 { 200 197 .name = "ipv4-frag", ··· 222 217 .dport = 8080, 223 218 }, 224 219 .flags = BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG, 220 + .retval = BPF_OK, 225 221 }, 226 222 { 227 223 .name = "ipv4-no-frag", ··· 245 239 .is_frag = true, 246 240 .is_first_frag = true, 247 241 }, 242 + .retval = BPF_OK, 248 243 }, 249 244 { 250 245 .name = "ipv6-frag", ··· 272 265 .dport = 8080, 273 266 }, 274 267 .flags = BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG, 268 + .retval = BPF_OK, 275 269 }, 276 270 { 277 271 .name = "ipv6-no-frag", ··· 295 287 .is_frag = true, 296 288 .is_first_frag = true, 297 289 }, 290 + .retval = BPF_OK, 298 291 }, 299 292 { 300 293 .name = "ipv6-flow-label", ··· 318 309 .dport = 8080, 319 310 .flow_label = __bpf_constant_htonl(0xbeeef), 320 311 }, 312 + .retval = BPF_OK, 321 313 }, 322 314 { 323 315 .name = "ipv6-no-flow-label", ··· 341 331 .flow_label = __bpf_constant_htonl(0xbeeef), 342 332 }, 343 333 .flags = BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL, 334 + .retval = BPF_OK, 344 335 }, 345 336 { 346 337 .name = "ipip-encap", ··· 370 359 .sport = 80, 371 360 .dport = 8080, 372 361 }, 362 + .retval = BPF_OK, 373 363 }, 374 364 { 375 365 .name = "ipip-no-encap", ··· 398 386 .is_encap = true, 399 387 }, 400 388 .flags = BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP, 389 + .retval = BPF_OK, 401 390 }, 402 391 }; 403 392 ··· 516 503 err = tx_tap(tap_fd, &tests[i].pkt, sizeof(tests[i].pkt)); 517 504 CHECK(err < 0, "tx_tap", "err %d errno %d\n", err, errno); 518 505 506 + /* check the stored flow_keys only if BPF_OK expected */ 507 + if (tests[i].retval != BPF_OK) 508 + continue; 509 + 519 510 err = bpf_map_lookup_elem(keys_fd, &key, &flow_keys); 520 511 ASSERT_OK(err, "bpf_map_lookup_elem"); 521 512 ··· 605 588 606 589 err = bpf_prog_test_run_opts(prog_fd, &topts); 607 590 ASSERT_OK(err, "test_run"); 608 - ASSERT_EQ(topts.retval, 1, "test_run retval"); 591 + ASSERT_EQ(topts.retval, tests[i].retval, "test_run retval"); 592 + 593 + /* check the resulting flow_keys only if BPF_OK returned */ 594 + if (topts.retval != BPF_OK) 595 + continue; 609 596 ASSERT_EQ(topts.data_size_out, sizeof(flow_keys), 610 597 "test_run data_size_out"); 611 598 CHECK_FLOW_KEYS(tests[i].name, flow_keys, tests[i].keys);
+1 -1
tools/testing/selftests/bpf/prog_tests/flow_dissector_load_bytes.c
··· 44 44 ASSERT_OK(err, "test_run"); 45 45 ASSERT_EQ(topts.data_size_out, sizeof(flow_keys), 46 46 "test_run data_size_out"); 47 - ASSERT_EQ(topts.retval, 1, "test_run retval"); 47 + ASSERT_EQ(topts.retval, BPF_OK, "test_run retval"); 48 48 49 49 if (fd >= -1) 50 50 close(fd);