1 // SPDX-License-Identifier: GPL-2.0 2 #include <test_progs.h> 3 #include <network_helpers.h> 4 #include "test_bpf_nf.skel.h" 5 #include "test_bpf_nf_fail.skel.h" 6 7 static char log_buf[1024 * 1024]; 8 9 struct { 10 const char *prog_name; 11 const char *err_msg; 12 } test_bpf_nf_fail_tests[] = { 13 { "alloc_release", "kernel function bpf_ct_release args#0 expected pointer to STRUCT nf_conn but" }, 14 { "insert_insert", "kernel function bpf_ct_insert_entry args#0 expected pointer to STRUCT nf_conn___init but" }, 15 { "lookup_insert", "kernel function bpf_ct_insert_entry args#0 expected pointer to STRUCT nf_conn___init but" }, 16 { "set_timeout_after_insert", "kernel function bpf_ct_set_timeout args#0 expected pointer to STRUCT nf_conn___init but" }, 17 { "set_status_after_insert", "kernel function bpf_ct_set_status args#0 expected pointer to STRUCT nf_conn___init but" }, 18 { "change_timeout_after_alloc", "kernel function bpf_ct_change_timeout args#0 expected pointer to STRUCT nf_conn but" }, 19 { "change_status_after_alloc", "kernel function bpf_ct_change_status args#0 expected pointer to STRUCT nf_conn but" }, 20 }; 21 22 enum { 23 TEST_XDP, 24 TEST_TC_BPF, 25 }; 26 27 static void test_bpf_nf_ct(int mode) 28 { 29 struct test_bpf_nf *skel; 30 int prog_fd, err; 31 LIBBPF_OPTS(bpf_test_run_opts, topts, 32 .data_in = &pkt_v4, 33 .data_size_in = sizeof(pkt_v4), 34 .repeat = 1, 35 ); 36 37 skel = test_bpf_nf__open_and_load(); 38 if (!ASSERT_OK_PTR(skel, "test_bpf_nf__open_and_load")) 39 return; 40 41 if (mode == TEST_XDP) 42 prog_fd = bpf_program__fd(skel->progs.nf_xdp_ct_test); 43 else 44 prog_fd = bpf_program__fd(skel->progs.nf_skb_ct_test); 45 46 err = bpf_prog_test_run_opts(prog_fd, &topts); 47 if (!ASSERT_OK(err, "bpf_prog_test_run")) 48 goto end; 49 50 ASSERT_EQ(skel->bss->test_einval_bpf_tuple, -EINVAL, "Test EINVAL for NULL bpf_tuple"); 51 ASSERT_EQ(skel->bss->test_einval_reserved, -EINVAL, "Test EINVAL for reserved not set to 0"); 52 ASSERT_EQ(skel->bss->test_einval_netns_id, -EINVAL, "Test EINVAL for netns_id < -1"); 53 ASSERT_EQ(skel->bss->test_einval_len_opts, -EINVAL, "Test EINVAL for len__opts != NF_BPF_CT_OPTS_SZ"); 54 ASSERT_EQ(skel->bss->test_eproto_l4proto, -EPROTO, "Test EPROTO for l4proto != TCP or UDP"); 55 ASSERT_EQ(skel->bss->test_enonet_netns_id, -ENONET, "Test ENONET for bad but valid netns_id"); 56 ASSERT_EQ(skel->bss->test_enoent_lookup, -ENOENT, "Test ENOENT for failed lookup"); 57 ASSERT_EQ(skel->bss->test_eafnosupport, -EAFNOSUPPORT, "Test EAFNOSUPPORT for invalid len__tuple"); 58 ASSERT_EQ(skel->data->test_alloc_entry, 0, "Test for alloc new entry"); 59 ASSERT_EQ(skel->data->test_insert_entry, 0, "Test for insert new entry"); 60 ASSERT_EQ(skel->data->test_succ_lookup, 0, "Test for successful lookup"); 61 /* allow some tolerance for test_delta_timeout value to avoid races. */ 62 ASSERT_GT(skel->bss->test_delta_timeout, 8, "Test for min ct timeout update"); 63 ASSERT_LE(skel->bss->test_delta_timeout, 10, "Test for max ct timeout update"); 64 /* expected status is IPS_SEEN_REPLY */ 65 ASSERT_EQ(skel->bss->test_status, 2, "Test for ct status update "); 66 end: 67 test_bpf_nf__destroy(skel); 68 } 69 70 static void test_bpf_nf_ct_fail(const char *prog_name, const char *err_msg) 71 { 72 LIBBPF_OPTS(bpf_object_open_opts, opts, .kernel_log_buf = log_buf, 73 .kernel_log_size = sizeof(log_buf), 74 .kernel_log_level = 1); 75 struct test_bpf_nf_fail *skel; 76 struct bpf_program *prog; 77 int ret; 78 79 skel = test_bpf_nf_fail__open_opts(&opts); 80 if (!ASSERT_OK_PTR(skel, "test_bpf_nf_fail__open")) 81 return; 82 83 prog = bpf_object__find_program_by_name(skel->obj, prog_name); 84 if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name")) 85 goto end; 86 87 bpf_program__set_autoload(prog, true); 88 89 ret = test_bpf_nf_fail__load(skel); 90 if (!ASSERT_ERR(ret, "test_bpf_nf_fail__load must fail")) 91 goto end; 92 93 if (!ASSERT_OK_PTR(strstr(log_buf, err_msg), "expected error message")) { 94 fprintf(stderr, "Expected: %s\n", err_msg); 95 fprintf(stderr, "Verifier: %s\n", log_buf); 96 } 97 98 end: 99 test_bpf_nf_fail__destroy(skel); 100 } 101 102 void test_bpf_nf(void) 103 { 104 int i; 105 if (test__start_subtest("xdp-ct")) 106 test_bpf_nf_ct(TEST_XDP); 107 if (test__start_subtest("tc-bpf-ct")) 108 test_bpf_nf_ct(TEST_TC_BPF); 109 for (i = 0; i < ARRAY_SIZE(test_bpf_nf_fail_tests); i++) { 110 if (test__start_subtest(test_bpf_nf_fail_tests[i].prog_name)) 111 test_bpf_nf_ct_fail(test_bpf_nf_fail_tests[i].prog_name, 112 test_bpf_nf_fail_tests[i].err_msg); 113 } 114 } 115