From patchwork Tue Apr 20 19:37:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kumar Kartikeya Dwivedi X-Patchwork-Id: 425020 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AB035C43461 for ; Tue, 20 Apr 2021 19:37:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 74C41613E4 for ; Tue, 20 Apr 2021 19:37:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233773AbhDTTiX (ORCPT ); Tue, 20 Apr 2021 15:38:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233734AbhDTTiU (ORCPT ); Tue, 20 Apr 2021 15:38:20 -0400 Received: from mail-pg1-x541.google.com (mail-pg1-x541.google.com [IPv6:2607:f8b0:4864:20::541]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF33BC06174A; Tue, 20 Apr 2021 12:37:48 -0700 (PDT) Received: by mail-pg1-x541.google.com with SMTP id q10so27379714pgj.2; Tue, 20 Apr 2021 12:37:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=oYCNET7iFKziKZ1Ub0jaHIxttSr8miY2y9P2JjnTAOQ=; b=Ux/XVioh+3znSi/ZaBcHUAS9qqZIutHCoG6V0g7a23qgHrTETG+4zVNZ0iSFeOAjpe pQpemcvLNw3jqC9XfUT3JktAuqBrR5zVjSz4SlLNy+EbzAe123Io8JwJ0t0dwZFgjt/P CH/DNEkCGow2E9f5GTmrkPSZDK+pk0on4lHVMiH7yC0AdpL59gsGPS5rUdSYC6Iam9iA s4KoP5C8EB3CuLrSFbwM3Q8htlDo+gOpXz/esSijBYq7xFMNLcZOplBRwEtEnEF+bqPs tb7F+/BUSiuMNPSMCjchJIFWBcf2mNvtelTxgxImmcpe6ZPyKjpFz6e0lJ66qTc4LsE2 W7Dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oYCNET7iFKziKZ1Ub0jaHIxttSr8miY2y9P2JjnTAOQ=; b=TqkZuZlmQZfoCOZOTpHK/FRzN2laJ9OO12Ldn63X71A3zuSbibSx/gtGTpgSl8gWJS eb0Jz1cyX0DSU/uKMQ8b87NmY1AMrp8Kjpm7h6vXcVCWheSOKwZXkzQKvbIw/8br2/as DzDToGpXTVnile336lxObIzm9KxAFt1CdOwb1GWdP6l4tQChj5aQ+CY+21XQ1pC+Zo7x AdzFkfWzU8hMtA3+O9/IzM6sWUMHbD92xXEHuYmLI6wGH5vcGgJ5sN9MnDTc0s6kDy88 s7RNjCd+Wrr3dbDJ+Q0KyUpSo62I1Kwb9NoJdsi0KUMESStuqcCa6jSMlTJn+S9ygx2t 5DWg== X-Gm-Message-State: AOAM533XERkt3LDWevCQ6cGW7l84xD+2eUkq2quLNjzLCY1XDf/HRpsI heFX9SC1R27SFdSd8HnjbbB9izBM7cMczQ== X-Google-Smtp-Source: ABdhPJwimD4+mYlbc3Jg4kNLHcZTh0AS3jr+Le7MYR1npPbPifXz2TeSq4DCymQ72NhVSplIiA9S2g== X-Received: by 2002:a17:90b:300f:: with SMTP id hg15mr6790218pjb.92.1618947468163; Tue, 20 Apr 2021 12:37:48 -0700 (PDT) Received: from localhost ([112.79.227.195]) by smtp.gmail.com with ESMTPSA id p10sm16861329pgn.85.2021.04.20.12.37.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Apr 2021 12:37:47 -0700 (PDT) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Kumar Kartikeya Dwivedi , =?utf-8?q?Toke_H=C3=B8ilan?= =?utf-8?q?d-J=C3=B8rgensen?= , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , netdev@vger.kernel.org Subject: [PATCH bpf-next v3 1/3] libbpf: add helpers for preparing netlink attributes Date: Wed, 21 Apr 2021 01:07:38 +0530 Message-Id: <20210420193740.124285-2-memxor@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210420193740.124285-1-memxor@gmail.com> References: <20210420193740.124285-1-memxor@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This change introduces a few helpers to wrap open coded attribute preparation in netlink.c. Every nested attribute's closure must happen using the helper nlattr_end_nested, which sets its length properly. NLA_F_NESTED is enforeced using nlattr_begin_nested helper. Other simple attributes can be added directly. The maxsz parameter corresponds to the size of the request structure which is being filled in, so for instance with req being: struct { struct nlmsghdr nh; struct tcmsg t; char buf[4096]; } req; Then, maxsz should be sizeof(req). This change also converts the open coded attribute preparation with the helpers. Note that the only failure the internal call to nlattr_add could result in the nested helper would be -EMSGSIZE, hence that is what we return to our caller. Reviewed-by: Toke Høiland-Jørgensen Signed-off-by: Kumar Kartikeya Dwivedi --- tools/lib/bpf/netlink.c | 37 ++++++++++++++----------------- tools/lib/bpf/nlattr.h | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c index d2cb28e9ef52..c79e30484e81 100644 --- a/tools/lib/bpf/netlink.c +++ b/tools/lib/bpf/netlink.c @@ -135,7 +135,7 @@ static int __bpf_set_link_xdp_fd_replace(int ifindex, int fd, int old_fd, __u32 flags) { int sock, seq = 0, ret; - struct nlattr *nla, *nla_xdp; + struct nlattr *nla; struct { struct nlmsghdr nh; struct ifinfomsg ifinfo; @@ -157,36 +157,31 @@ static int __bpf_set_link_xdp_fd_replace(int ifindex, int fd, int old_fd, req.ifinfo.ifi_index = ifindex; /* started nested attribute for XDP */ - nla = (struct nlattr *)(((char *)&req) - + NLMSG_ALIGN(req.nh.nlmsg_len)); - nla->nla_type = NLA_F_NESTED | IFLA_XDP; - nla->nla_len = NLA_HDRLEN; + nla = nlattr_begin_nested(&req.nh, sizeof(req), IFLA_XDP); + if (!nla) { + ret = -EMSGSIZE; + goto cleanup; + } /* add XDP fd */ - nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len); - nla_xdp->nla_type = IFLA_XDP_FD; - nla_xdp->nla_len = NLA_HDRLEN + sizeof(int); - memcpy((char *)nla_xdp + NLA_HDRLEN, &fd, sizeof(fd)); - nla->nla_len += nla_xdp->nla_len; + ret = nlattr_add(&req.nh, sizeof(req), IFLA_XDP_FD, &fd, sizeof(fd)); + if (ret < 0) + goto cleanup; /* if user passed in any flags, add those too */ if (flags) { - nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len); - nla_xdp->nla_type = IFLA_XDP_FLAGS; - nla_xdp->nla_len = NLA_HDRLEN + sizeof(flags); - memcpy((char *)nla_xdp + NLA_HDRLEN, &flags, sizeof(flags)); - nla->nla_len += nla_xdp->nla_len; + ret = nlattr_add(&req.nh, sizeof(req), IFLA_XDP_FLAGS, &flags, sizeof(flags)); + if (ret < 0) + goto cleanup; } if (flags & XDP_FLAGS_REPLACE) { - nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len); - nla_xdp->nla_type = IFLA_XDP_EXPECTED_FD; - nla_xdp->nla_len = NLA_HDRLEN + sizeof(old_fd); - memcpy((char *)nla_xdp + NLA_HDRLEN, &old_fd, sizeof(old_fd)); - nla->nla_len += nla_xdp->nla_len; + ret = nlattr_add(&req.nh, sizeof(req), IFLA_XDP_EXPECTED_FD, &flags, sizeof(flags)); + if (ret < 0) + goto cleanup; } - req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len); + nlattr_end_nested(&req.nh, nla); if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) { ret = -errno; diff --git a/tools/lib/bpf/nlattr.h b/tools/lib/bpf/nlattr.h index 6cc3ac91690f..1c94cdb6e89d 100644 --- a/tools/lib/bpf/nlattr.h +++ b/tools/lib/bpf/nlattr.h @@ -10,7 +10,10 @@ #define __LIBBPF_NLATTR_H #include +#include +#include #include + /* avoid multiple definition of netlink features */ #define __LINUX_NETLINK_H @@ -103,4 +106,49 @@ int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype, int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh); +static inline struct nlattr *nla_data(struct nlattr *nla) +{ + return (struct nlattr *)((char *)nla + NLA_HDRLEN); +} + +static inline struct nlattr *nh_tail(struct nlmsghdr *nh) +{ + return (struct nlattr *)((char *)nh + NLMSG_ALIGN(nh->nlmsg_len)); +} + +static inline int nlattr_add(struct nlmsghdr *nh, size_t maxsz, int type, + const void *data, int len) +{ + struct nlattr *nla; + + if (NLMSG_ALIGN(nh->nlmsg_len) + NLA_ALIGN(NLA_HDRLEN + len) > maxsz) + return -EMSGSIZE; + if ((!data && len) || (data && !len)) + return -EINVAL; + + nla = nh_tail(nh); + nla->nla_type = type; + nla->nla_len = NLA_HDRLEN + len; + if (data) + memcpy(nla_data(nla), data, len); + nh->nlmsg_len = NLMSG_ALIGN(nh->nlmsg_len) + NLA_ALIGN(nla->nla_len); + return 0; +} + +static inline struct nlattr *nlattr_begin_nested(struct nlmsghdr *nh, + size_t maxsz, int type) +{ + struct nlattr *tail; + + tail = nh_tail(nh); + if (nlattr_add(nh, maxsz, type | NLA_F_NESTED, NULL, 0)) + return NULL; + return tail; +} + +static inline void nlattr_end_nested(struct nlmsghdr *nh, struct nlattr *tail) +{ + tail->nla_len = (char *)nh_tail(nh) - (char *)tail; +} + #endif /* __LIBBPF_NLATTR_H */ From patchwork Tue Apr 20 19:37:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kumar Kartikeya Dwivedi X-Patchwork-Id: 425019 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EE3A1C43461 for ; Tue, 20 Apr 2021 19:37:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B9DF4613E2 for ; Tue, 20 Apr 2021 19:37:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233792AbhDTTi2 (ORCPT ); Tue, 20 Apr 2021 15:38:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35454 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233787AbhDTTi1 (ORCPT ); Tue, 20 Apr 2021 15:38:27 -0400 Received: from mail-pg1-x542.google.com (mail-pg1-x542.google.com [IPv6:2607:f8b0:4864:20::542]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B823C06138A; Tue, 20 Apr 2021 12:37:55 -0700 (PDT) Received: by mail-pg1-x542.google.com with SMTP id y32so27374032pga.11; Tue, 20 Apr 2021 12:37:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ggSnWKXUJLE6iHcOs0eUWOJm8Vh5SmESAPT+YojILTs=; b=ZkUENDZH0Dfzhu3yQhk2ckxI79uQ/YN8MF/ZzdjvRJYty1qkQSl+5AWbCMoeIiPYzJ xa53ShKa4XGlecfYUHuXLEOXyDAead5kghszWVKO6DYH0wT0O7oSD3uudMwQhgpQ6t7x YZ2JRGwGjfw9p5iURmNzh+fUNF4XZNdRY6imEjYA+jYG40Nd1QbgjqarXa4MWjJkKGr2 73pbGVvacPqraYGSmn9S2n4oRPUzw0NQ+9WUEDLu2Oo+oSW6S7kAJ26GHkstKIbqu4ac dIaNWgM69ht4UuBWBJJQELSEvq5kMuadquT04dpQlTS4/JZI8bPeFAUFekW0v5ixeuXt y6NQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ggSnWKXUJLE6iHcOs0eUWOJm8Vh5SmESAPT+YojILTs=; b=RX7+hQuvKF9rOosO102EX/Ium9nWNE8GbbWoxsVw5Sfu4joLUTyIAJbc0Ti2M7vN/O KuKrw02Wps4FRQLR6wCGBMZJgYcd3D19HqXJIJPSkCy8vVMQAxqoZpgHDFe/x+0f38Lr 9gyv4RR9CYrKudzcVLeP90aw7LgYmLhSCN9PO2irD+gSV97hmMz9tpJj/gpyA6FAh7KA TZLo0zn6pvOGZgDSiAI856B3ujUxOiZH0RzTK6d8h7nXJM3gBH8xb4OIoGoUzw5TSKpN 8plZhoFfFlJ31oTT+4MFN7ncNZra4U4Tb1oC8DHrjCref9Kq4hE+aRAMrY5bp0sOFtMk sOSQ== X-Gm-Message-State: AOAM532mBhRPGO44S0d0i1+7xNQgKtV4wCWABcXTmnqBqlEkKWNlO6kU 9WWud50tEFuuWlGgKcn+xiCfi7MFsEw8DA== X-Google-Smtp-Source: ABdhPJz1Vyx87oJ0VKjUuP7ZO7nPIrrWmgbTjs1OprdgRfIjMp9RwNjF1walaOY49VihKz2QUFy/sQ== X-Received: by 2002:a62:4d86:0:b029:252:c889:2dd8 with SMTP id a128-20020a624d860000b0290252c8892dd8mr27264030pfb.41.1618947474750; Tue, 20 Apr 2021 12:37:54 -0700 (PDT) Received: from localhost ([112.79.227.195]) by smtp.gmail.com with ESMTPSA id p22sm3063937pjg.39.2021.04.20.12.37.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Apr 2021 12:37:54 -0700 (PDT) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Kumar Kartikeya Dwivedi , =?utf-8?q?Toke_H=C3=B8ilan?= =?utf-8?q?d-J=C3=B8rgensen?= , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , netdev@vger.kernel.org Subject: [PATCH bpf-next v3 3/3] libbpf: add selftests for TC-BPF API Date: Wed, 21 Apr 2021 01:07:40 +0530 Message-Id: <20210420193740.124285-4-memxor@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210420193740.124285-1-memxor@gmail.com> References: <20210420193740.124285-1-memxor@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This adds some basic tests for the low level bpf_tc_* API. Reviewed-by: Toke Høiland-Jørgensen Signed-off-by: Kumar Kartikeya Dwivedi --- .../selftests/bpf/prog_tests/test_tc_bpf.c | 169 ++++++++++++++++++ .../selftests/bpf/progs/test_tc_bpf_kern.c | 12 ++ 2 files changed, 181 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_tc_bpf_kern.c diff --git a/tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c b/tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c new file mode 100644 index 000000000000..563a3944553c --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/test_tc_bpf.c @@ -0,0 +1,169 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define LO_IFINDEX 1 + +static int test_tc_internal(int fd, __u32 parent_id) +{ + DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 10, + .class_id = TC_H_MAKE(1UL << 16, 1)); + struct bpf_tc_attach_id id = {}; + struct bpf_tc_info info = {}; + int ret; + + ret = bpf_tc_attach(fd, LO_IFINDEX, parent_id, &opts, &id); + if (!ASSERT_EQ(ret, 0, "bpf_tc_attach")) + return ret; + + ret = bpf_tc_get_info(LO_IFINDEX, parent_id, &id, &info); + if (!ASSERT_EQ(ret, 0, "bpf_tc_get_info")) + goto end; + + if (!ASSERT_EQ(info.id.handle, id.handle, "handle mismatch") || + !ASSERT_EQ(info.id.priority, id.priority, "priority mismatch") || + !ASSERT_EQ(info.id.handle, 1, "handle incorrect") || + !ASSERT_EQ(info.chain_index, 0, "chain_index incorrect") || + !ASSERT_EQ(info.id.priority, 10, "priority incorrect") || + !ASSERT_EQ(info.class_id, TC_H_MAKE(1UL << 16, 1), + "class_id incorrect") || + !ASSERT_EQ(info.protocol, ETH_P_ALL, "protocol incorrect")) + goto end; + + opts.replace = true; + ret = bpf_tc_attach(fd, LO_IFINDEX, parent_id, &opts, &id); + if (!ASSERT_EQ(ret, 0, "bpf_tc_attach in replace mode")) + return ret; + + /* Demonstrate changing attributes */ + opts.class_id = TC_H_MAKE(1UL << 16, 2); + + ret = bpf_tc_attach(fd, LO_IFINDEX, parent_id, &opts, &id); + if (!ASSERT_EQ(ret, 0, "bpf_tc attach in replace mode")) + goto end; + + ret = bpf_tc_get_info(LO_IFINDEX, parent_id, &id, &info); + if (!ASSERT_EQ(ret, 0, "bpf_tc_get_info")) + goto end; + + if (!ASSERT_EQ(info.class_id, TC_H_MAKE(1UL << 16, 2), + "class_id incorrect after replace")) + goto end; + if (!ASSERT_EQ(info.bpf_flags & TCA_BPF_FLAG_ACT_DIRECT, 1, + "direct action mode not set")) + goto end; + +end: + ret = bpf_tc_detach(LO_IFINDEX, parent_id, &id); + ASSERT_EQ(ret, 0, "detach failed"); + return ret; +} + +int test_tc_info(int fd) +{ + DECLARE_LIBBPF_OPTS(bpf_tc_opts, opts, .handle = 1, .priority = 10, + .class_id = TC_H_MAKE(1UL << 16, 1)); + struct bpf_tc_attach_id id = {}, old; + struct bpf_tc_info info = {}; + int ret; + + ret = bpf_tc_attach(fd, LO_IFINDEX, BPF_TC_CLSACT_INGRESS, &opts, &id); + if (!ASSERT_EQ(ret, 0, "bpf_tc_attach")) + return ret; + old = id; + + ret = bpf_tc_get_info(LO_IFINDEX, BPF_TC_CLSACT_INGRESS, &id, &info); + if (!ASSERT_EQ(ret, 0, "bpf_tc_get_info")) + goto end_old; + + if (!ASSERT_EQ(info.id.handle, id.handle, "handle mismatch") || + !ASSERT_EQ(info.id.priority, id.priority, "priority mismatch") || + !ASSERT_EQ(info.id.handle, 1, "handle incorrect") || + !ASSERT_EQ(info.chain_index, 0, "chain_index incorrect") || + !ASSERT_EQ(info.id.priority, 10, "priority incorrect") || + !ASSERT_EQ(info.class_id, TC_H_MAKE(1UL << 16, 1), + "class_id incorrect") || + !ASSERT_EQ(info.protocol, ETH_P_ALL, "protocol incorrect")) + goto end_old; + + /* choose a priority */ + opts.priority = 0; + ret = bpf_tc_attach(fd, LO_IFINDEX, BPF_TC_CLSACT_INGRESS, &opts, &id); + if (!ASSERT_EQ(ret, 0, "bpf_tc_attach")) + goto end_old; + + ret = bpf_tc_get_info(LO_IFINDEX, BPF_TC_CLSACT_INGRESS, &id, &info); + if (!ASSERT_EQ(ret, 0, "bpf_tc_get_info")) + goto end; + + if (!ASSERT_NEQ(id.priority, old.priority, "filter priority mismatch")) + goto end; + if (!ASSERT_EQ(info.id.priority, id.priority, "priority mismatch")) + goto end; + +end: + ret = bpf_tc_detach(LO_IFINDEX, BPF_TC_CLSACT_INGRESS, &id); + ASSERT_EQ(ret, 0, "detach failed"); +end_old: + ret = bpf_tc_detach(LO_IFINDEX, BPF_TC_CLSACT_INGRESS, &old); + ASSERT_EQ(ret, 0, "detach failed"); + return ret; +} + +void test_test_tc_bpf(void) +{ + const char *file = "./test_tc_bpf_kern.o"; + struct bpf_program *clsp; + struct bpf_object *obj; + int cls_fd, ret; + + obj = bpf_object__open(file); + if (!ASSERT_OK_PTR(obj, "bpf_object__open")) + return; + + clsp = bpf_object__find_program_by_title(obj, "classifier"); + if (!ASSERT_OK_PTR(clsp, "bpf_object__find_program_by_title")) + goto end; + + ret = bpf_object__load(obj); + if (!ASSERT_EQ(ret, 0, "bpf_object__load")) + goto end; + + cls_fd = bpf_program__fd(clsp); + + system("tc qdisc del dev lo clsact"); + + ret = test_tc_internal(cls_fd, BPF_TC_CLSACT_INGRESS); + if (!ASSERT_EQ(ret, 0, "test_tc_internal INGRESS")) + goto end; + + if (!ASSERT_EQ(system("tc qdisc del dev lo clsact"), 0, + "clsact qdisc delete failed")) + goto end; + + ret = test_tc_info(cls_fd); + if (!ASSERT_EQ(ret, 0, "test_tc_info")) + goto end; + + if (!ASSERT_EQ(system("tc qdisc del dev lo clsact"), 0, + "clsact qdisc delete failed")) + goto end; + + ret = test_tc_internal(cls_fd, BPF_TC_CLSACT_EGRESS); + if (!ASSERT_EQ(ret, 0, "test_tc_internal EGRESS")) + goto end; + + ASSERT_EQ(system("tc qdisc del dev lo clsact"), 0, + "clsact qdisc delete failed"); + +end: + bpf_object__close(obj); +} diff --git a/tools/testing/selftests/bpf/progs/test_tc_bpf_kern.c b/tools/testing/selftests/bpf/progs/test_tc_bpf_kern.c new file mode 100644 index 000000000000..18a3a7ed924a --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_tc_bpf_kern.c @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +/* Dummy prog to test TC-BPF API */ + +SEC("classifier") +int cls(struct __sk_buff *skb) +{ + return 0; +}