bpftool: Add ability to specify custom skeleton object name
authorAndrii Nakryiko <andrii@kernel.org>
Thu, 18 Mar 2021 19:40:32 +0000 (12:40 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 18 Mar 2021 23:14:23 +0000 (16:14 -0700)
Add optional name OBJECT_NAME parameter to `gen skeleton` command to override
default object name, normally derived from input file name. This allows much
more flexibility during build time.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210318194036.3521577-9-andrii@kernel.org
tools/bpf/bpftool/Documentation/bpftool-gen.rst
tools/bpf/bpftool/bash-completion/bpftool
tools/bpf/bpftool/gen.c

index 84cf063..d4e7338 100644 (file)
@@ -19,7 +19,7 @@ SYNOPSIS
 GEN COMMANDS
 =============
 
-|      **bpftool** **gen skeleton** *FILE*
+|      **bpftool** **gen skeleton** *FILE* [**name** *OBJECT_NAME*]
 |      **bpftool** **gen help**
 
 DESCRIPTION
@@ -75,10 +75,13 @@ DESCRIPTION
                  specific maps, programs, etc.
 
                  As part of skeleton, few custom functions are generated.
-                 Each of them is prefixed with object name, derived from
-                 object file name. I.e., if BPF object file name is
-                 **example.o**, BPF object name will be **example**. The
-                 following custom functions are provided in such case:
+                 Each of them is prefixed with object name. Object name can
+                 either be derived from object file name, i.e., if BPF object
+                 file name is **example.o**, BPF object name will be
+                 **example**. Object name can be also specified explicitly
+                 through **name** *OBJECT_NAME* parameter. The following
+                 custom functions are provided (assuming **example** as
+                 the object name):
 
                  - **example__open** and **example__open_opts**.
                    These functions are used to instantiate skeleton. It
index fdffbc6..bf7b4bd 100644 (file)
@@ -982,7 +982,16 @@ _bpftool()
         gen)
             case $command in
                 skeleton)
-                    _filedir
+                    case $prev in
+                        $command)
+                            _filedir
+                            return 0
+                            ;;
+                        *)
+                            _bpftool_once_attr 'name'
+                            return 0
+                            ;;
+                    esac
                     ;;
                 *)
                     [[ $prev == $object ]] && \
index 4033c46..9bff89a 100644 (file)
@@ -273,7 +273,7 @@ static int do_skeleton(int argc, char **argv)
        char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")];
        size_t i, map_cnt = 0, prog_cnt = 0, file_sz, mmap_sz;
        DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
-       char obj_name[MAX_OBJ_NAME_LEN], *obj_data;
+       char obj_name[MAX_OBJ_NAME_LEN] = "", *obj_data;
        struct bpf_object *obj = NULL;
        const char *file, *ident;
        struct bpf_program *prog;
@@ -288,6 +288,28 @@ static int do_skeleton(int argc, char **argv)
        }
        file = GET_ARG();
 
+       while (argc) {
+               if (!REQ_ARGS(2))
+                       return -1;
+
+               if (is_prefix(*argv, "name")) {
+                       NEXT_ARG();
+
+                       if (obj_name[0] != '\0') {
+                               p_err("object name already specified");
+                               return -1;
+                       }
+
+                       strncpy(obj_name, *argv, MAX_OBJ_NAME_LEN - 1);
+                       obj_name[MAX_OBJ_NAME_LEN - 1] = '\0';
+               } else {
+                       p_err("unknown arg %s", *argv);
+                       return -1;
+               }
+
+               NEXT_ARG();
+       }
+
        if (argc) {
                p_err("extra unknown arguments");
                return -1;
@@ -310,7 +332,8 @@ static int do_skeleton(int argc, char **argv)
                p_err("failed to mmap() %s: %s", file, strerror(errno));
                goto out;
        }
-       get_obj_name(obj_name, file);
+       if (obj_name[0] == '\0')
+               get_obj_name(obj_name, file);
        opts.object_name = obj_name;
        obj = bpf_object__open_mem(obj_data, file_sz, &opts);
        if (IS_ERR(obj)) {
@@ -599,7 +622,7 @@ static int do_help(int argc, char **argv)
        }
 
        fprintf(stderr,
-               "Usage: %1$s %2$s skeleton FILE\n"
+               "Usage: %1$s %2$s skeleton FILE [name OBJECT_NAME]\n"
                "       %1$s %2$s help\n"
                "\n"
                "       " HELP_SPEC_OPTIONS "\n"