From: Hangbin Liu <liuhangbin@gmail.com>
To: netdev@vger.kernel.org
Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>,
David Ahern <dsahern@kernel.org>,
toke@redhat.com, stephen@networkplumber.org,
Hangbin Liu <liuhangbin@gmail.com>
Subject: [PATCH iproute2-next] libbpf: add xdp program name support
Date: Tue, 5 Jul 2022 12:25:01 +0800 [thread overview]
Message-ID: <20220705042501.187198-1-liuhangbin@gmail.com> (raw)
In bpf program, only the program name is unique. Before this patch, if there
are multiple programs with the same section name, only the first program
will be attached. With program name support, users could specify the exact
program they want to attach.
Note this feature is only supported when iproute2 build with libbpf.
Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
---
include/bpf_util.h | 1 +
ip/iplink.c | 4 ++++
lib/bpf_legacy.c | 11 +++++++++--
lib/bpf_libbpf.c | 26 +++++++++++++++++++++++---
man/man8/ip-link.8.in | 9 ++++++++-
5 files changed, 45 insertions(+), 6 deletions(-)
diff --git a/include/bpf_util.h b/include/bpf_util.h
index abb96275..6a5f8ec6 100644
--- a/include/bpf_util.h
+++ b/include/bpf_util.h
@@ -68,6 +68,7 @@ enum bpf_mode {
struct bpf_cfg_in {
const char *object;
const char *section;
+ const char *prog_name;
const char *uds;
enum bpf_prog_type type;
enum bpf_mode mode;
diff --git a/ip/iplink.c b/ip/iplink.c
index c64721bc..b98c1694 100644
--- a/ip/iplink.c
+++ b/ip/iplink.c
@@ -107,7 +107,11 @@ void iplink_usage(void)
" [ node_guid EUI64 ]\n"
" [ port_guid EUI64 ] ]\n"
" [ { xdp | xdpgeneric | xdpdrv | xdpoffload } { off |\n"
+#ifdef HAVE_LIBBPF
+ " object FILE [ { section | program } NAME ] [ verbose ] |\n"
+#else
" object FILE [ section NAME ] [ verbose ] |\n"
+#endif
" pinned FILE } ]\n"
" [ master DEVICE ][ vrf NAME ]\n"
" [ nomaster ]\n"
diff --git a/lib/bpf_legacy.c b/lib/bpf_legacy.c
index 9bf7c1c4..4fabdcc8 100644
--- a/lib/bpf_legacy.c
+++ b/lib/bpf_legacy.c
@@ -831,7 +831,7 @@ static int bpf_obj_pinned(const char *pathname, enum bpf_prog_type type)
static int bpf_do_parse(struct bpf_cfg_in *cfg, const bool *opt_tbl)
{
- const char *file, *section, *uds_name;
+ const char *file, *section, *uds_name, *prog_name;
bool verbose = false;
int i, ret, argc;
char **argv;
@@ -862,7 +862,7 @@ static int bpf_do_parse(struct bpf_cfg_in *cfg, const bool *opt_tbl)
}
NEXT_ARG();
- file = section = uds_name = NULL;
+ file = section = uds_name = prog_name = NULL;
if (cfg->mode == EBPF_OBJECT || cfg->mode == EBPF_PINNED) {
file = *argv;
NEXT_ARG_FWD();
@@ -899,6 +899,12 @@ static int bpf_do_parse(struct bpf_cfg_in *cfg, const bool *opt_tbl)
NEXT_ARG_FWD();
}
+ if (argc > 0 && strcmp(*argv, "program") == 0) {
+ NEXT_ARG();
+ prog_name = *argv;
+ NEXT_ARG_FWD();
+ }
+
if (__bpf_prog_meta[cfg->type].may_uds_export) {
uds_name = getenv(BPF_ENV_UDS);
if (argc > 0 && !uds_name &&
@@ -936,6 +942,7 @@ static int bpf_do_parse(struct bpf_cfg_in *cfg, const bool *opt_tbl)
cfg->argc = argc;
cfg->argv = argv;
cfg->verbose = verbose;
+ cfg->prog_name = prog_name;
return ret;
}
diff --git a/lib/bpf_libbpf.c b/lib/bpf_libbpf.c
index 7b16ee71..e1c211a1 100644
--- a/lib/bpf_libbpf.c
+++ b/lib/bpf_libbpf.c
@@ -254,6 +254,22 @@ static bool bpf_map_is_offload_neutral(const struct bpf_map *map)
return bpf_map__type(map) == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
}
+static bool find_prog_to_attach(struct bpf_program *prog,
+ struct bpf_program *exist_prog,
+ const char *section, const char *prog_name)
+{
+ if (exist_prog)
+ return false;
+
+ /* We have default section name 'prog'. So do not check
+ * section name if there already has program name.
+ */
+ if (prog_name)
+ return !strcmp(bpf_program__name(prog), prog_name);
+ else
+ return !strcmp(get_bpf_program__section_name(prog), section);
+}
+
static int load_bpf_object(struct bpf_cfg_in *cfg)
{
struct bpf_program *p, *prog = NULL;
@@ -278,8 +294,9 @@ static int load_bpf_object(struct bpf_cfg_in *cfg)
}
bpf_object__for_each_program(p, obj) {
- bool prog_to_attach = !prog && cfg->section &&
- !strcmp(get_bpf_program__section_name(p), cfg->section);
+ bool prog_to_attach = find_prog_to_attach(p, prog,
+ cfg->section,
+ cfg->prog_name);
/* Only load the programs that will either be subsequently
* attached or inserted into a tail call map */
@@ -304,7 +321,10 @@ static int load_bpf_object(struct bpf_cfg_in *cfg)
}
if (!prog) {
- fprintf(stderr, "object file doesn't contain sec %s\n", cfg->section);
+ if (cfg->prog_name)
+ fprintf(stderr, "object file doesn't contain prog %s\n", cfg->prog_name);
+ else
+ fprintf(stderr, "object file doesn't contain sec %s\n", cfg->section);
return -ENOENT;
}
diff --git a/man/man8/ip-link.8.in b/man/man8/ip-link.8.in
index 3dbcdbb6..c45c1062 100644
--- a/man/man8/ip-link.8.in
+++ b/man/man8/ip-link.8.in
@@ -149,7 +149,7 @@ ip-link \- network device configuration
.in +8
.BR object
.IR FILE
-.RB "[ " section
+.RB "[ { " section " | " program " } "
.IR NAME " ]"
.RB "[ " verbose " ] |"
.br
@@ -2342,6 +2342,13 @@ to be passed with the
.B object
option.
+.BI program " NAME "
+- Specifies the BPF program name that need to be attached. When the program
+name is specified, the section name parameter will be ignored. This option
+only works when iproute2 build with
+.B libbpf
+support.
+
.BI verbose
- Act in verbose mode. For example, even in case of success, this will
print the verifier log in case a program was loaded from a BPF ELF file.
--
2.35.1
next reply other threads:[~2022-07-05 4:25 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-05 4:25 Hangbin Liu [this message]
2022-07-08 15:20 ` [PATCH iproute2-next] libbpf: add xdp program name support patchwork-bot+netdevbpf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220705042501.187198-1-liuhangbin@gmail.com \
--to=liuhangbin@gmail.com \
--cc=andrii.nakryiko@gmail.com \
--cc=dsahern@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=stephen@networkplumber.org \
--cc=toke@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).