Commit c4122665 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov
Browse files

bpftool: Add ability to specify custom skeleton object name



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: default avatarAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210318194036.3521577-9-andrii@kernel.org
parent 8fd27bf6
Loading
Loading
Loading
Loading
+8 −5
Original line number Diff line number Diff line
@@ -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
+10 −1
Original line number Diff line number Diff line
@@ -982,7 +982,16 @@ _bpftool()
        gen)
            case $command in
                skeleton)
                    case $prev in
                        $command)
                            _filedir
                            return 0
                            ;;
                        *)
                            _bpftool_once_attr 'name'
                            return 0
                            ;;
                    esac
                    ;;
                *)
                    [[ $prev == $object ]] && \
+26 −3
Original line number Diff line number Diff line
@@ -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,6 +332,7 @@ static int do_skeleton(int argc, char **argv)
		p_err("failed to mmap() %s: %s", file, strerror(errno));
		goto out;
	}
	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);
@@ -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"