From: Jakub Kicinski <jakub.kicinski@netronome.com>
To: alexei.starovoitov@gmail.com, daniel@iogearbox.net
Cc: oss-drivers@netronome.com, netdev@vger.kernel.org,
Jakub Kicinski <jakub.kicinski@netronome.com>
Subject: [PATCH bpf-next 03/11] tools: bpftool: refactor argument parsing for prog load
Date: Tue, 3 Jul 2018 19:54:09 -0700 [thread overview]
Message-ID: <20180704025417.8848-4-jakub.kicinski@netronome.com> (raw)
In-Reply-To: <20180704025417.8848-1-jakub.kicinski@netronome.com>
Add a new macro for printing more informative message than straight
usage() when parameters are missing, and use it for prog do_load().
Save the object and pin path argument to variables for clarity.
Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com>
---
tools/bpf/bpftool/main.h | 15 +++++++++++++++
tools/bpf/bpftool/prog.c | 11 +++++++----
2 files changed, 22 insertions(+), 4 deletions(-)
diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
index d39f7ef01d23..15b6c49ae533 100644
--- a/tools/bpf/bpftool/main.h
+++ b/tools/bpf/bpftool/main.h
@@ -50,6 +50,21 @@
#define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); })
#define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); })
#define BAD_ARG() ({ p_err("what is '%s'?", *argv); -1; })
+#define GET_ARG() ({ argc--; *argv++; })
+#define REQ_ARGS(cnt) \
+ ({ \
+ int _cnt = (cnt); \
+ bool _res; \
+ \
+ if (argc < _cnt) { \
+ p_err("'%s' needs at least %d arguments, %d found", \
+ argv[-1], _cnt, argc); \
+ _res = false; \
+ } else { \
+ _res = true; \
+ } \
+ _res; \
+ })
#define ERR_MAX_LEN 1024
diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c
index a740da99d477..a5ef46c59029 100644
--- a/tools/bpf/bpftool/prog.c
+++ b/tools/bpf/bpftool/prog.c
@@ -681,18 +681,21 @@ static int do_pin(int argc, char **argv)
static int do_load(int argc, char **argv)
{
+ const char *objfile, *pinfile;
struct bpf_object *obj;
int prog_fd;
- if (argc != 2)
- usage();
+ if (!REQ_ARGS(2))
+ return -1;
+ objfile = GET_ARG();
+ pinfile = GET_ARG();
- if (bpf_prog_load(argv[0], BPF_PROG_TYPE_UNSPEC, &obj, &prog_fd)) {
+ if (bpf_prog_load(objfile, BPF_PROG_TYPE_UNSPEC, &obj, &prog_fd)) {
p_err("failed to load program");
return -1;
}
- if (do_pin_fd(prog_fd, argv[1]))
+ if (do_pin_fd(prog_fd, pinfile))
goto err_close_obj;
if (json_output)
--
2.17.1
next prev parent reply other threads:[~2018-07-04 2:54 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-04 2:54 [PATCH bpf-next 00/11] tools: bpf: extend bpftool prog load Jakub Kicinski
2018-07-04 2:54 ` [PATCH bpf-next 01/11] selftests/bpf: remove duplicated word from test offloads Jakub Kicinski
2018-07-04 2:54 ` [PATCH bpf-next 02/11] selftests/bpf: add Error: prefix in check_extack helper Jakub Kicinski
2018-07-04 2:54 ` Jakub Kicinski [this message]
2018-07-04 2:54 ` [PATCH bpf-next 04/11] tools: bpftool: add support for loading programs for offload Jakub Kicinski
2018-07-04 2:54 ` [PATCH bpf-next 05/11] tools: libbpf: expose the prog type guessing from section name logic Jakub Kicinski
2018-07-04 2:54 ` [PATCH bpf-next 06/11] tools: bpftool: allow users to specify program type for prog load Jakub Kicinski
2018-07-04 2:54 ` [PATCH bpf-next 07/11] tools: libbpf: recognize offload neutral maps Jakub Kicinski
2018-07-04 2:54 ` [PATCH bpf-next 08/11] tools: libbpf: add extended attributes version of bpf_object__open() Jakub Kicinski
2018-07-04 2:54 ` [PATCH bpf-next 09/11] tools: bpftool: reimplement bpf_prog_load() for prog load Jakub Kicinski
2018-07-04 2:54 ` [PATCH bpf-next 10/11] tools: libbpf: allow map reuse Jakub Kicinski
2018-07-04 2:54 ` [PATCH bpf-next 11/11] tools: bpftool: allow reuse of maps with bpftool prog load Jakub Kicinski
2018-07-05 8:35 ` Daniel Borkmann
2018-07-05 22:57 ` Jakub Kicinski
2018-07-06 7:16 ` Daniel Borkmann
2018-07-06 15:30 ` Jakub Kicinski
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=20180704025417.8848-4-jakub.kicinski@netronome.com \
--to=jakub.kicinski@netronome.com \
--cc=alexei.starovoitov@gmail.com \
--cc=daniel@iogearbox.net \
--cc=netdev@vger.kernel.org \
--cc=oss-drivers@netronome.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