Re: [PATCH bpf-next v2 11/18] bpftool: Add btf enum64 support

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





On 5/17/22 4:38 PM, Andrii Nakryiko wrote:
On Fri, May 13, 2022 at 8:13 PM Yonghong Song <yhs@xxxxxx> wrote:

Add BTF_KIND_ENUM64 support.
For example, the following enum is defined in uapi bpf.h.
   $ cat core.c
   enum A {
         BPF_F_INDEX_MASK                = 0xffffffffULL,
         BPF_F_CURRENT_CPU               = BPF_F_INDEX_MASK,
         BPF_F_CTXLEN_MASK               = (0xfffffULL << 32),
   } g;
Compiled with
   clang -target bpf -O2 -g -c core.c
Using bpftool to dump types and generate format C file:
   $ bpftool btf dump file core.o
   ...
   [1] ENUM64 'A' size=8 vlen=3
         'BPF_F_INDEX_MASK' val=4294967295ULL
         'BPF_F_CURRENT_CPU' val=4294967295ULL
         'BPF_F_CTXLEN_MASK' val=4503595332403200ULL
   $ bpftool btf dump file core.o format c
   ...
   enum A {
         BPF_F_INDEX_MASK = 4294967295ULL,
         BPF_F_CURRENT_CPU = 4294967295ULL,
         BPF_F_CTXLEN_MASK = 4503595332403200ULL,
   };
   ...

The 64bit value is represented properly in BTF and C dump.

Acked-by: Andrii Nakryiko <andrii@xxxxxxxxxx>
Signed-off-by: Yonghong Song <yhs@xxxxxx>
---
  tools/bpf/bpftool/btf.c        | 49 ++++++++++++++++++++++++++++++++--
  tools/bpf/bpftool/btf_dumper.c | 29 ++++++++++++++++++++
  tools/bpf/bpftool/gen.c        |  1 +
  3 files changed, 77 insertions(+), 2 deletions(-)

diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c
index a2c665beda87..9e5db870fe53 100644
--- a/tools/bpf/bpftool/btf.c
+++ b/tools/bpf/bpftool/btf.c
@@ -40,6 +40,7 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = {
         [BTF_KIND_FLOAT]        = "FLOAT",
         [BTF_KIND_DECL_TAG]     = "DECL_TAG",
         [BTF_KIND_TYPE_TAG]     = "TYPE_TAG",
+       [BTF_KIND_ENUM64]       = "ENUM64",
  };

  struct btf_attach_point {
@@ -228,10 +229,54 @@ static int dump_btf_type(const struct btf *btf, __u32 id,
                         if (json_output) {
                                 jsonw_start_object(w);
                                 jsonw_string_field(w, "name", name);
-                               jsonw_uint_field(w, "val", v->val);
+                               if (btf_kflag(t))
+                                       jsonw_int_field(w, "val", v->val);
+                               else
+                                       jsonw_uint_field(w, "val", v->val);
                                 jsonw_end_object(w);
                         } else {
-                               printf("\n\t'%s' val=%u", name, v->val);
+                               if (btf_kflag(t))
+                                       printf("\n\t'%s' val=%d", name, v->val);
+                               else
+                                       printf("\n\t'%s' val=%u", name, v->val);
+                       }
+               }
+               if (json_output)
+                       jsonw_end_array(w);
+               break;
+       }
+       case BTF_KIND_ENUM64: {
+               const struct btf_enum64 *v = btf_enum64(t);
+               __u16 vlen = btf_vlen(t);
+               int i;
+
+               if (json_output) {
+                       jsonw_uint_field(w, "size", t->size);
+                       jsonw_uint_field(w, "vlen", vlen);
+                       jsonw_name(w, "values");
+                       jsonw_start_array(w);
+               } else {
+                       printf(" size=%u vlen=%u", t->size, vlen);
+               }
+               for (i = 0; i < vlen; i++, v++) {
+                       const char *name = btf_str(btf, v->name_off);
+                       __u64 val = ((__u64)v->val_hi32 << 32) | v->val_lo32;
+
+                       if (json_output) {
+                               jsonw_start_object(w);
+                               jsonw_string_field(w, "name", name);

forgot emitting kflag itself (both in bpftool and in selftests), let's
add that for both enum and enum64?

will do. I will also check other places whether I missed or not.


+                               if (btf_kflag(t))
+                                       jsonw_int_field(w, "val", val);
+                               else
+                                       jsonw_uint_field(w, "val", val);
+                               jsonw_end_object(w);
+                       } else {
+                               if (btf_kflag(t))
+                                       printf("\n\t'%s' val=%lldLL", name,
+                                              (unsigned long long)val);
+                               else
+                                       printf("\n\t'%s' val=%lluULL", name,
+                                              (unsigned long long)val);
                         }
                 }
                 if (json_output)

[...]



[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux