tools: bpftool: allow users to specify program type for prog load
authorJakub Kicinski <jakub.kicinski@netronome.com>
Tue, 10 Jul 2018 21:43:00 +0000 (14:43 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Wed, 11 Jul 2018 20:13:33 +0000 (22:13 +0200)
Sometimes program section names don't match with libbpf's expectation.
In particular XDP's default section names differ between libbpf and
iproute2.  Allow users to pass program type on command line.  Name
the types like the libbpf expected section names.

Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
tools/bpf/bpftool/Documentation/bpftool-prog.rst
tools/bpf/bpftool/bash-completion/bpftool
tools/bpf/bpftool/prog.c

index 41723c6acaa6087fac6e6fd8d1caf27e56370bf2..e53e1ad2caf064cbf5b3e001781f013b6a6a7a4a 100644 (file)
@@ -24,10 +24,19 @@ MAP COMMANDS
 |      **bpftool** **prog dump xlated** *PROG* [{**file** *FILE* | **opcodes** | **visual**}]
 |      **bpftool** **prog dump jited**  *PROG* [{**file** *FILE* | **opcodes**}]
 |      **bpftool** **prog pin** *PROG* *FILE*
-|      **bpftool** **prog load** *OBJ* *FILE* [**dev** *NAME*]
+|      **bpftool** **prog load** *OBJ* *FILE* [**type** *TYPE*] [**dev** *NAME*]
 |      **bpftool** **prog help**
 |
 |      *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* }
+|      *TYPE* := {
+|              **socket** | **kprobe** | **kretprobe** | **classifier** | **action** |
+|              **tracepoint** | **raw_tracepoint** | **xdp** | **perf_event** | **cgroup/skb** |
+|              **cgroup/sock** | **cgroup/dev** | **lwt_in** | **lwt_out** | **lwt_xmit** |
+|              **lwt_seg6local** | **sockops** | **sk_skb** | **sk_msg** | **lirc_mode2** |
+|              **cgroup/bind4** | **cgroup/bind6** | **cgroup/post_bind4** | **cgroup/post_bind6** |
+|              **cgroup/connect4** | **cgroup/connect6** | **cgroup/sendmsg4** | **cgroup/sendmsg6**
+|      }
+
 
 DESCRIPTION
 ===========
@@ -64,8 +73,10 @@ DESCRIPTION
 
                  Note: *FILE* must be located in *bpffs* mount.
 
-       **bpftool prog load** *OBJ* *FILE* [**dev** *NAME*]
+       **bpftool prog load** *OBJ* *FILE* [**type** *TYPE*] [**dev** *NAME*]
                  Load bpf program from binary *OBJ* and pin as *FILE*.
+                 **type** is optional, if not specified program type will be
+                 inferred from section names.
                  If **dev** *NAME* is specified program will be loaded onto
                  given networking device (offload).
 
index 238c2f80092a20675d06a77b9304404fb02fa4f7..caf8711993be4d1bd4cbd20ec77245eab81a05d0 100644 (file)
@@ -274,11 +274,17 @@ _bpftool()
                     fi
 
                     case $prev in
+                        type)
+                            COMPREPLY=( $( compgen -W "socket kprobe kretprobe classifier action tracepoint raw_tracepoint xdp perf_event cgroup/skb cgroup/sock cgroup/dev lwt_in lwt_out lwt_xmit lwt_seg6local sockops sk_skb sk_msg lirc_mode2 cgroup/bind4 cgroup/bind6 cgroup/connect4 cgroup/connect6 cgroup/sendmsg4 cgroup/sendmsg6 cgroup/post_bind4 cgroup/post_bind6" -- \
+                                                   "$cur" ) )
+                            return 0
+                            ;;
                         dev)
                             _sysfs_get_netdevs
                             return 0
                             ;;
                         *)
+                            _bpftool_once_attr 'type'
                             _bpftool_once_attr 'dev'
                             return 0
                             ;;
index 21c74de7156f3911e4a15b1352c36177a1c7e593..98695585bbb6b75930dbd35cbcae26d2da8c7a9f 100644 (file)
@@ -688,6 +688,7 @@ static int do_load(int argc, char **argv)
        const char *objfile, *pinfile;
        struct bpf_object *obj;
        int prog_fd;
+       int err;
 
        if (!REQ_ARGS(2))
                return -1;
@@ -695,7 +696,37 @@ static int do_load(int argc, char **argv)
        pinfile = GET_ARG();
 
        while (argc) {
-               if (is_prefix(*argv, "dev")) {
+               if (is_prefix(*argv, "type")) {
+                       char *type;
+
+                       NEXT_ARG();
+
+                       if (attr.prog_type != BPF_PROG_TYPE_UNSPEC) {
+                               p_err("program type already specified");
+                               return -1;
+                       }
+                       if (!REQ_ARGS(1))
+                               return -1;
+
+                       /* Put a '/' at the end of type to appease libbpf */
+                       type = malloc(strlen(*argv) + 2);
+                       if (!type) {
+                               p_err("mem alloc failed");
+                               return -1;
+                       }
+                       *type = 0;
+                       strcat(type, *argv);
+                       strcat(type, "/");
+
+                       err = libbpf_prog_type_by_name(type, &attr.prog_type,
+                                                      &attr.expected_attach_type);
+                       free(type);
+                       if (err < 0) {
+                               p_err("unknown program type '%s'", *argv);
+                               return err;
+                       }
+                       NEXT_ARG();
+               } else if (is_prefix(*argv, "dev")) {
                        NEXT_ARG();
 
                        if (attr.ifindex) {
@@ -713,7 +744,7 @@ static int do_load(int argc, char **argv)
                        }
                        NEXT_ARG();
                } else {
-                       p_err("expected no more arguments or 'dev', got: '%s'?",
+                       p_err("expected no more arguments, 'type' or 'dev', got: '%s'?",
                              *argv);
                        return -1;
                }
@@ -753,10 +784,17 @@ static int do_help(int argc, char **argv)
                "       %s %s dump xlated PROG [{ file FILE | opcodes | visual }]\n"
                "       %s %s dump jited  PROG [{ file FILE | opcodes }]\n"
                "       %s %s pin   PROG FILE\n"
-               "       %s %s load  OBJ  FILE [dev NAME]\n"
+               "       %s %s load  OBJ  FILE [type TYPE] [dev NAME]\n"
                "       %s %s help\n"
                "\n"
                "       " HELP_SPEC_PROGRAM "\n"
+               "       TYPE := { socket | kprobe | kretprobe | classifier | action |\n"
+               "                 tracepoint | raw_tracepoint | xdp | perf_event | cgroup/skb |\n"
+               "                 cgroup/sock | cgroup/dev | lwt_in | lwt_out | lwt_xmit |\n"
+               "                 lwt_seg6local | sockops | sk_skb | sk_msg | lirc_mode2 |\n"
+               "                 cgroup/bind4 | cgroup/bind6 | cgroup/post_bind4 |\n"
+               "                 cgroup/post_bind6 | cgroup/connect4 | cgroup/connect6 |\n"
+               "                 cgroup/sendmsg4 | cgroup/sendmsg6 }\n"
                "       " HELP_SPEC_OPTIONS "\n"
                "",
                bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],