From patchwork Wed May 22 19:50:41 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 1103623 Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=fb.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=fb.com header.i=@fb.com header.b="E9EGmSyr"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 458NY21y8nz9sP9 for ; Thu, 23 May 2019 05:51:02 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730890AbfEVTvA (ORCPT ); Wed, 22 May 2019 15:51:00 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:59824 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2387894AbfEVTu6 (ORCPT ); Wed, 22 May 2019 15:50:58 -0400 Received: from pps.filterd (m0001303.ppops.net [127.0.0.1]) by m0001303.ppops.net (8.16.0.27/8.16.0.27) with SMTP id x4MJlHSN023848 for ; Wed, 22 May 2019 12:50:57 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : mime-version : content-type; s=facebook; bh=fVdcKk701UmWd0bJeAU0VAf/QzGizTnrH7kqg2MVfLQ=; b=E9EGmSyrsbNtAuarRhTHm0tff4kaNTJAWiYtAtK04eTizI8IQm41ek9RYLo+x7FJs98i 6x9rb6oSJb3wzcBndQN5UjAEf1JBs5OAJQT+/uPOKd/FqybAutGhpH+hR/w95rxcnDm5 F+Faoc8CwZGeZU7DJWePlaV0RM6G8amGOqM= Received: from maileast.thefacebook.com ([163.114.130.16]) by m0001303.ppops.net with ESMTP id 2sn5ta1rm7-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 22 May 2019 12:50:57 -0700 Received: from mx-out.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1713.5; Wed, 22 May 2019 12:50:56 -0700 Received: by dev101.prn2.facebook.com (Postfix, from userid 137359) id 0FED0862334; Wed, 22 May 2019 12:50:55 -0700 (PDT) Smtp-Origin-Hostprefix: dev From: Andrii Nakryiko Smtp-Origin-Hostname: dev101.prn2.facebook.com To: , , , , , CC: Andrii Nakryiko Smtp-Origin-Cluster: prn2c23 Subject: [PATCH bpf-next 00/12] BTF-to-C converter Date: Wed, 22 May 2019 12:50:41 -0700 Message-ID: <20190522195053.4017624-1-andriin@fb.com> X-Mailer: git-send-email 2.17.1 X-FB-Internal: Safe MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-05-22_11:, , signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1905220138 X-FB-Internal: deliver Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This patch set adds BTF-to-C dumping APIs to libbpf, allowing to output a subset of BTF types as a compilable C type definitions. This is useful by itself, as raw BTF output is not easy to inspect and comprehend. But it's also a big part of BPF CO-RE (compile once - run everywhere) initiative aimed at allowing to write relocatable BPF programs, that won't require on-the-host kernel headers (and would be able to inspect internal kernel structures, not exposed through kernel headers). This patch set consists of three groups of patches and one pre-patch, with the BTF-to-C dumper API depending on the first two groups. Pre-patch #1 fixes issue with libbpf_internal.h. btf__parse_elf() API patches: - patch #2 adds btf__parse_elf() API to libbpf, allowing to load BTF and/or BTF.ext from ELF file; - patch #3 utilizies btf__parse_elf() from bpftool for `btf dump file` command; - patch #4 switches test_btf.c to use btf__parse_elf() to check for presence of BTF data in object file. libbpf's internal hashmap patches: - patch #5 adds resizeable non-thread safe generic hashmap to libbpf; - patch #6 adds tests for that hashmap; - patch #7 migrates btf_dedup()'s dedup_table to use hashmap w/ APPEND. BTF-to-C dumper API patches: - patch #8 adds btf_dump APIs with all the logic for laying out type definitions in correct order and emitting C syntax for them; - patch #9 adds lots of tests for common and quirky parts of C type system; - patch #10 adds support for C-syntax btf dumping to bpftool; - patch #11 updates bpftool documentation to mention C-syntax dump option; - patch #12 update bash-completion for btf dump sub-command. v0->v1: - fix bug in hashmap__for_each_bucket_entry() not handling empty hashmap; - removed `btf dump`-specific libbpf logging hook up (Quentin has more generic patchset); - change btf__parse_elf() to always load .BTF and return it as a result, with .BTF.ext being optional and returned through struct btf_ext** arg (Alexei); - endianness check to use __BYTE_ORDER__ (Alexei); - bool:1 to __u8:1 in type_aux_state (Alexei); - added HASHMAP_APPEND strategy to hashmap, changed hashmap__for_each_key_entry() to also check for key equality during iteration (multimap iteration for key); - added new tests for empty hashmap and hashmap as a multimap; - tried to clarify weak/strong dependency ordering comments (Alexei) - btf dump test's expected output - support better commenting aproach (Alexei); - added bash-completion for a new "c" option (Alexei). Andrii Nakryiko (12): libbpf: ensure libbpf.h is included along libbpf_internal.h libbpf: add btf__parse_elf API to load .BTF and .BTF.ext bpftool: use libbpf's btf__parse_elf API selftests/bpf: use btf__parse_elf to check presence of BTF/BTF.ext libbpf: add resizable non-thread safe internal hashmap selftests/bpf: add tests for libbpf's hashmap libbpf: switch btf_dedup() to hashmap for dedup table libbpf: add btf_dump API for BTF-to-C conversion selftests/bpf: add btf_dump BTF-to-C conversion tests bpftool: add C output format option to btf dump subcommand bpftool/docs: add description of btf dump C option bpftool: update bash-completion w/ new c option for btf dump .../bpf/bpftool/Documentation/bpftool-btf.rst | 5 +- tools/bpf/bpftool/bash-completion/bpftool | 22 +- tools/bpf/bpftool/btf.c | 150 +- tools/lib/bpf/Build | 4 +- tools/lib/bpf/btf.c | 330 ++-- tools/lib/bpf/btf.h | 19 + tools/lib/bpf/btf_dump.c | 1336 +++++++++++++++++ tools/lib/bpf/hashmap.c | 229 +++ tools/lib/bpf/hashmap.h | 173 +++ tools/lib/bpf/libbpf.map | 4 + tools/lib/bpf/libbpf_internal.h | 2 + tools/testing/selftests/bpf/.gitignore | 2 + tools/testing/selftests/bpf/Makefile | 3 +- .../bpf/progs/btf_dump_test_case_bitfields.c | 92 ++ .../bpf/progs/btf_dump_test_case_multidim.c | 35 + .../progs/btf_dump_test_case_namespacing.c | 73 + .../bpf/progs/btf_dump_test_case_ordering.c | 63 + .../bpf/progs/btf_dump_test_case_packing.c | 75 + .../bpf/progs/btf_dump_test_case_padding.c | 111 ++ .../bpf/progs/btf_dump_test_case_syntax.c | 229 +++ tools/testing/selftests/bpf/test_btf.c | 71 +- tools/testing/selftests/bpf/test_btf_dump.c | 143 ++ tools/testing/selftests/bpf/test_hashmap.c | 382 +++++ 23 files changed, 3275 insertions(+), 278 deletions(-) create mode 100644 tools/lib/bpf/btf_dump.c create mode 100644 tools/lib/bpf/hashmap.c create mode 100644 tools/lib/bpf/hashmap.h create mode 100644 tools/testing/selftests/bpf/progs/btf_dump_test_case_bitfields.c create mode 100644 tools/testing/selftests/bpf/progs/btf_dump_test_case_multidim.c create mode 100644 tools/testing/selftests/bpf/progs/btf_dump_test_case_namespacing.c create mode 100644 tools/testing/selftests/bpf/progs/btf_dump_test_case_ordering.c create mode 100644 tools/testing/selftests/bpf/progs/btf_dump_test_case_packing.c create mode 100644 tools/testing/selftests/bpf/progs/btf_dump_test_case_padding.c create mode 100644 tools/testing/selftests/bpf/progs/btf_dump_test_case_syntax.c create mode 100644 tools/testing/selftests/bpf/test_btf_dump.c create mode 100644 tools/testing/selftests/bpf/test_hashmap.c