Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <test_progs.h>
0003 #include <network_helpers.h>
0004 #include "test_xdp_context_test_run.skel.h"
0005 
0006 void test_xdp_context_error(int prog_fd, struct bpf_test_run_opts opts,
0007                 __u32 data_meta, __u32 data, __u32 data_end,
0008                 __u32 ingress_ifindex, __u32 rx_queue_index,
0009                 __u32 egress_ifindex)
0010 {
0011     struct xdp_md ctx = {
0012         .data = data,
0013         .data_end = data_end,
0014         .data_meta = data_meta,
0015         .ingress_ifindex = ingress_ifindex,
0016         .rx_queue_index = rx_queue_index,
0017         .egress_ifindex = egress_ifindex,
0018     };
0019     int err;
0020 
0021     opts.ctx_in = &ctx;
0022     opts.ctx_size_in = sizeof(ctx);
0023     err = bpf_prog_test_run_opts(prog_fd, &opts);
0024     ASSERT_EQ(errno, EINVAL, "errno-EINVAL");
0025     ASSERT_ERR(err, "bpf_prog_test_run");
0026 }
0027 
0028 void test_xdp_context_test_run(void)
0029 {
0030     struct test_xdp_context_test_run *skel = NULL;
0031     char data[sizeof(pkt_v4) + sizeof(__u32)];
0032     char bad_ctx[sizeof(struct xdp_md) + 1];
0033     struct xdp_md ctx_in, ctx_out;
0034     DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
0035                 .data_in = &data,
0036                 .data_size_in = sizeof(data),
0037                 .ctx_out = &ctx_out,
0038                 .ctx_size_out = sizeof(ctx_out),
0039                 .repeat = 1,
0040         );
0041     int err, prog_fd;
0042 
0043     skel = test_xdp_context_test_run__open_and_load();
0044     if (!ASSERT_OK_PTR(skel, "skel"))
0045         return;
0046     prog_fd = bpf_program__fd(skel->progs.xdp_context);
0047 
0048     /* Data past the end of the kernel's struct xdp_md must be 0 */
0049     bad_ctx[sizeof(bad_ctx) - 1] = 1;
0050     opts.ctx_in = bad_ctx;
0051     opts.ctx_size_in = sizeof(bad_ctx);
0052     err = bpf_prog_test_run_opts(prog_fd, &opts);
0053     ASSERT_EQ(errno, E2BIG, "extradata-errno");
0054     ASSERT_ERR(err, "bpf_prog_test_run(extradata)");
0055 
0056     *(__u32 *)data = XDP_PASS;
0057     *(struct ipv4_packet *)(data + sizeof(__u32)) = pkt_v4;
0058     opts.ctx_in = &ctx_in;
0059     opts.ctx_size_in = sizeof(ctx_in);
0060     memset(&ctx_in, 0, sizeof(ctx_in));
0061     ctx_in.data_meta = 0;
0062     ctx_in.data = sizeof(__u32);
0063     ctx_in.data_end = ctx_in.data + sizeof(pkt_v4);
0064     err = bpf_prog_test_run_opts(prog_fd, &opts);
0065     ASSERT_OK(err, "bpf_prog_test_run(valid)");
0066     ASSERT_EQ(opts.retval, XDP_PASS, "valid-retval");
0067     ASSERT_EQ(opts.data_size_out, sizeof(pkt_v4), "valid-datasize");
0068     ASSERT_EQ(opts.ctx_size_out, opts.ctx_size_in, "valid-ctxsize");
0069     ASSERT_EQ(ctx_out.data_meta, 0, "valid-datameta");
0070     ASSERT_EQ(ctx_out.data, 0, "valid-data");
0071     ASSERT_EQ(ctx_out.data_end, sizeof(pkt_v4), "valid-dataend");
0072 
0073     /* Meta data's size must be a multiple of 4 */
0074     test_xdp_context_error(prog_fd, opts, 0, 1, sizeof(data), 0, 0, 0);
0075 
0076     /* data_meta must reference the start of data */
0077     test_xdp_context_error(prog_fd, opts, 4, sizeof(__u32), sizeof(data),
0078                    0, 0, 0);
0079 
0080     /* Meta data must be 32 bytes or smaller */
0081     test_xdp_context_error(prog_fd, opts, 0, 36, sizeof(data), 0, 0, 0);
0082 
0083     /* Total size of data must match data_end - data_meta */
0084     test_xdp_context_error(prog_fd, opts, 0, sizeof(__u32),
0085                    sizeof(data) - 1, 0, 0, 0);
0086     test_xdp_context_error(prog_fd, opts, 0, sizeof(__u32),
0087                    sizeof(data) + 1, 0, 0, 0);
0088 
0089     /* RX queue cannot be specified without specifying an ingress */
0090     test_xdp_context_error(prog_fd, opts, 0, sizeof(__u32), sizeof(data),
0091                    0, 1, 0);
0092 
0093     /* Interface 1 is always the loopback interface which always has only
0094      * one RX queue (index 0). This makes index 1 an invalid rx queue index
0095      * for interface 1.
0096      */
0097     test_xdp_context_error(prog_fd, opts, 0, sizeof(__u32), sizeof(data),
0098                    1, 1, 0);
0099 
0100     /* The egress cannot be specified */
0101     test_xdp_context_error(prog_fd, opts, 0, sizeof(__u32), sizeof(data),
0102                    0, 0, 1);
0103 
0104     test_xdp_context_test_run__destroy(skel);
0105 }