From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753432AbdLHKeK (ORCPT ); Fri, 8 Dec 2017 05:34:10 -0500 Received: from mail-wm0-f65.google.com ([74.125.82.65]:40674 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753182AbdLHKd5 (ORCPT ); Fri, 8 Dec 2017 05:33:57 -0500 X-Google-Smtp-Source: AGs4zMaEoy2fEwmW3MVNwqLJZREQFGdvWiWCXYpp/0z1ghpT1vy95VcIPBFYKqygZu6EtbiFVghz7Q== Subject: Re: [PATCH v2 net-next 3/4] bpftool: implement prog load command To: Roman Gushchin , netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kernel-team@fb.com, ast@kernel.org, daniel@iogearbox.net, jakub.kicinski@netronome.com, kafai@fb.com, David Ahern References: <20171207183909.16240-1-guro@fb.com> <20171207183909.16240-4-guro@fb.com> From: Quentin Monnet Message-ID: Date: Fri, 8 Dec 2017 10:33:54 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: <20171207183909.16240-4-guro@fb.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 2017-12-07 18:39 UTC+0000 ~ Roman Gushchin > Add the prog load command to load a bpf program from a specified > binary file and pin it to bpffs. > > Usage description and examples are given in the corresponding man > page. > > Syntax: > $ bpftool prog load SOURCE_FILE FILE > > FILE is a non-existing file on bpffs. > > Signed-off-by: Roman Gushchin > Cc: Alexei Starovoitov > Cc: Daniel Borkmann > Cc: Jakub Kicinski > Cc: Martin KaFai Lau > Cc: Quentin Monnet > Cc: David Ahern > --- > tools/bpf/bpftool/Documentation/bpftool-prog.rst | 10 +++- > tools/bpf/bpftool/Documentation/bpftool.rst | 2 +- > tools/bpf/bpftool/common.c | 71 +++++++++++++----------- > tools/bpf/bpftool/main.h | 1 + > tools/bpf/bpftool/prog.c | 31 ++++++++++- > 5 files changed, 81 insertions(+), 34 deletions(-) > > diff --git a/tools/bpf/bpftool/Documentation/bpftool-prog.rst b/tools/bpf/bpftool/Documentation/bpftool-prog.rst > index 36e8d1c3c40d..827b415f8ab6 100644 > --- a/tools/bpf/bpftool/Documentation/bpftool-prog.rst > +++ b/tools/bpf/bpftool/Documentation/bpftool-prog.rst > @@ -15,7 +15,7 @@ SYNOPSIS > *OPTIONS* := { { **-j** | **--json** } [{ **-p** | **--pretty** }] | { **-f** | **--bpffs** } } > > *COMMANDS* := > - { **show** | **dump xlated** | **dump jited** | **pin** | **help** } > + { **show** | **dump xlated** | **dump jited** | **pin** | **load** | **help** } > > MAP COMMANDS > ============= > @@ -24,6 +24,7 @@ MAP COMMANDS > | **bpftool** **prog dump xlated** *PROG* [{**file** *FILE* | **opcodes**}] > | **bpftool** **prog dump jited** *PROG* [{**file** *FILE* | **opcodes**}] > | **bpftool** **prog pin** *PROG* *FILE* > +| **bpftool** **prog load** *SRC* *FILE* > | **bpftool** **prog help** > | > | *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* } > @@ -57,6 +58,11 @@ DESCRIPTION > > Note: *FILE* must be located in *bpffs* mount. > > + **bpftool prog load** *SRC* *FILE* > + Load bpf program from binary *SRC* and pin as *FILE*. > + > + Note: *FILE* must be located in *bpffs* mount. > + > **bpftool prog help** > Print short help message. > > @@ -126,8 +132,10 @@ EXAMPLES > | > | **# mount -t bpf none /sys/fs/bpf/** > | **# bpftool prog pin id 10 /sys/fs/bpf/prog** > +| **# bpftool prog load ./my_prog.o /sys/fs/bpf/prog2** > | **# ls -l /sys/fs/bpf/** > | -rw------- 1 root root 0 Jul 22 01:43 prog > +| -rw------- 1 root root 0 Dec 07 17:23 prog2 Nit: would you mind using a date closer to the first one? Just from a reader's point of view it might look strange to see the files apparently created with two successive commands having such a difference between their creation times. > > **# bpftool prog dum jited pinned /sys/fs/bpf/prog opcodes** > > diff --git a/tools/bpf/bpftool/Documentation/bpftool.rst b/tools/bpf/bpftool/Documentation/bpftool.rst > index 926c03d5a8da..f547a0c0aa34 100644 > --- a/tools/bpf/bpftool/Documentation/bpftool.rst > +++ b/tools/bpf/bpftool/Documentation/bpftool.rst > @@ -26,7 +26,7 @@ SYNOPSIS > | **pin** | **help** } > > *PROG-COMMANDS* := { **show** | **dump jited** | **dump xlated** | **pin** > - | **help** } > + | **load** | **help** } > > DESCRIPTION > =========== […] > diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c > index ad619b96c276..bac5d81e2ff0 100644 > --- a/tools/bpf/bpftool/prog.c > +++ b/tools/bpf/bpftool/prog.c > @@ -45,6 +45,7 @@ > #include > > #include > +#include > > #include "main.h" > #include "disasm.h" > @@ -635,6 +636,32 @@ static int do_pin(int argc, char **argv) > return err; > } > > +static int do_load(int argc, char **argv) > +{ > + struct bpf_object *obj; > + int prog_fd; > + > + if (argc != 2) { > + usage(); > + return -1; > + } No need to return here, usage() exits from the program. > + > + if (bpf_prog_load(argv[0], BPF_PROG_TYPE_UNSPEC, &obj, &prog_fd)) { > + p_err("failed to load program\n"); > + return -1; > + } > + > + if (do_pin_fd(prog_fd, argv[1])) { > + p_err("failed to pin program\n"); > + return -1; > + } > + > + if (json_output) > + jsonw_null(json_wtr); > + > + return 0; > +} > + > static int do_help(int argc, char **argv) > { > if (json_output) { > @@ -647,13 +674,14 @@ static int do_help(int argc, char **argv) > " %s %s dump xlated PROG [{ file FILE | opcodes }]\n" > " %s %s dump jited PROG [{ file FILE | opcodes }]\n" > " %s %s pin PROG FILE\n" > + " %s %s load SRC FILE\n" > " %s %s help\n" > "\n" > " " HELP_SPEC_PROGRAM "\n" > " " HELP_SPEC_OPTIONS "\n" > "", > bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2], > - bin_name, argv[-2], bin_name, argv[-2]); > + bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]); > > return 0; > } > @@ -663,6 +691,7 @@ static const struct cmd cmds[] = { > { "help", do_help }, > { "dump", do_dump }, > { "pin", do_pin }, > + { "load", do_load }, > { 0 } > }; > >