From patchwork Tue Mar 15 22:15:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12781886 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F2970C433EF for ; Tue, 15 Mar 2022 22:15:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231623AbiCOWQd (ORCPT ); Tue, 15 Mar 2022 18:16:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240840AbiCOWQc (ORCPT ); Tue, 15 Mar 2022 18:16:32 -0400 Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F34D5577B for ; Tue, 15 Mar 2022 15:15:19 -0700 (PDT) Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.1.2/8.16.1.2) with ESMTP id 22FLfkuD009029 for ; Tue, 15 Mar 2022 15:15:18 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : subject : date : message-id : references : in-reply-to : content-type : content-transfer-encoding : mime-version; s=facebook; bh=/DnVhiE3vtRvuA3Jusro+q582zGMKeebATYn/qcZxSM=; b=RlH09Ds4O8ddEqnUo5pYJCtTzk+gE4piT3oVPFjmx9BT50A/TJYWweseYA/R+B+PiPJX usKfvLsEPO9TRMaixz+d+fndzUeYnd5GWxdXM73qng6Ia+X7i8kqJWnhik3Sqzhv2HM0 N4pU8xT1yrijVh8ar/30RdWZwWpsrS1XRMU= Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2177.outbound.protection.outlook.com [104.47.58.177]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3eu2brghmn-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 15 Mar 2022 15:15:18 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TZFB/d/ryOgIEoRzSsfZpKucy77ikUAh2Etc84BYPRPTncGxSWuNSP2RRAgil7oUnV27d+Sy4NDMrVkdCpmJlk7ltjqZ7UOAWuQzyci5K/gjNa/vx7CD6XSFkm7p4ozegShrYp6moOMUKHl9iSXh0qDTkUXbgYXkctVYYAc2mOwtntP55UldCLpEe7wHZ4FnnfpyflLlO9q0NhZHniigXwoJ5RcFruJW+9qB8R9EbLFMpPLWnkwzbS6g3fy45dTVGbQsUcIdfaZ1zF8M26P95oW70m1safw6QjwoJGeczb2eMlnV6e7xXjUvyVRt+fN/xtckC3+9zHBnZc2c1r18cQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=/DnVhiE3vtRvuA3Jusro+q582zGMKeebATYn/qcZxSM=; b=U60ieu7AIbr2v0pJOwKCjpHjAW5yLzw11bh7qy+OrxaU2mYA3EDMvST2L6aQzHtpM0blUcjQCzAD2Em6dsACbZ8OVCia82qcQnmN/XrSLU1pa0mSw+zhPyd1hYNwc4p7i8hYaXrsh13T0ZEPkNG6VlVipB63TknsQ9hNcQm0/+V2sYKB17dFSuO0u4I/l4BLy+RWBygHmAIWCyKXnydBigrFg/L6OUANlCmO8bgRPOX8bja++VkG/Gz1BW+Rbs+VCl7Z1ZqzPGhl1xRkZNEYSQT/eYWMVbhY+Hu+48lWIJhsClukV87KcLjuOI4sP160d18w73vgSgDFN8XDUOj8TA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=fb.com; dmarc=pass action=none header.from=fb.com; dkim=pass header.d=fb.com; arc=none Received: from SJ0PR15MB5154.namprd15.prod.outlook.com (2603:10b6:a03:423::6) by BYAPR15MB2695.namprd15.prod.outlook.com (2603:10b6:a03:150::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5061.28; Tue, 15 Mar 2022 22:15:13 +0000 Received: from SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3]) by SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::90c1:20f8:4fc6:d7a3%6]) with mapi id 15.20.5081.014; Tue, 15 Mar 2022 22:15:13 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next v3 3/5] libbpf: add subskeleton scaffolding Thread-Topic: [PATCH bpf-next v3 3/5] libbpf: add subskeleton scaffolding Thread-Index: AQHYOLojqAQIwWhdUEimrWTBFQsXiw== Date: Tue, 15 Mar 2022 22:15:12 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 7a47d0c7-e480-4f9b-31bc-08da06d14716 x-ms-traffictypediagnostic: BYAPR15MB2695:EE_ x-microsoft-antispam-prvs: x-fb-source: Internal x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: uPtNibVXDplg1bORNhlrI796YEE79HS+BVs/6igUln4SNl/u6kfOgxvHYbyatOOWHpUo0stwqlUoFe0MyhA9BB4a2BFMEM5OWZKx6JYCTStjThoz6Kh2Sx69G/hSVHHB3Kg1XmYQFBHfAYYPlUHQju8G890mgIiEH2T59AW9usgrfh1DFDka861X+UAkKyawk4pTP2leBGEBG0jxxISKUS4HWAdhh6sqw//jt/nVxOopD3bBKcFLVDSis5ymYeGCRcZUR67pmiOL7dviJ+0V6uaPWgofDEOigqbnjMFQUoHf32x5LhszQpnu/0qNES+DnIUIUGFWOrhuVtNzuzeocIxee/T4ycNuDej6Ix0PkSqJ2bzYEm9ooYqoXdrovl75+zyGVzrEomtrinew9juRMefhdr7Rd5IXemPn00DJwooawnwu/+5VZbKDf81UkBtsebZsozHLjPx27uxCtDAlobO+vn+y76O7b9jqwTY+pyrkovpq0d10rlLhnYfk8by7djDwN3RUK/IXq1Yj7iBx8gtnO9HuhRx4waHbu5mO3sjNcoYF+9OHH0fsp6F5ZBvkYl8KcTZgxUeAc5RIjjaROzocmo6dIv3Hhgzaa6pPz0mDovxIlq7nBG88Z54EQFDIiEMl4oIA53Jrs9Am7ej3U1jcc64wE8KZt60ju/Uw+F48hS4zp4zCFF2LMtPKwaWpx235v/Cmwvj+be5OB0eTIg== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ0PR15MB5154.namprd15.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(66446008)(66946007)(6512007)(6506007)(2616005)(64756008)(38100700002)(8676002)(76116006)(66556008)(91956017)(122000001)(66476007)(2906002)(186003)(83380400001)(316002)(110136005)(6486002)(38070700005)(71200400001)(508600001)(86362001)(36756003)(8936002)(5660300002);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?wj93ecyLo0F0h15varjfxuu?= =?iso-8859-1?q?h2Ls4Ke3ktIi61EkOfZezOwkp4YPmzPL/+GF0FASRX7/M5Q3M7LMTzC5nsUW?= =?iso-8859-1?q?McatuTbDjI7lejsLGK1LdnfdVIJ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Oc6g4?= =?iso-8859-1?q?q6LXIrz45eNtl7jqVPoZXWGUW5adkIbKcyby/lizeIj5BNOAJPwoPrf93XGx?= =?iso-8859-1?q?T2yBh1P8S/hdUaAIDlnoYMoAFLn+lnDZJT6pG86BaN+CtQ9+QtErfAXad4Af?= =?iso-8859-1?q?lYJKujpdFMRvp6rwp5xvQAWDUk5jQhi7lbucbv9R+Sd5eNtyHU8r6zESrOF6?= =?iso-8859-1?q?ForISA8XRj4Hq98fsGyOgF54VxaAWeHgiUtXHSq7bin2zkt1ZmyRS9Wg7KOm?= =?iso-8859-1?q?xBQREaoabsNbLw0z8/rjbazXKFdZonefKgTLgjc6fu1Oazd8tXggkNTYOail?= =?iso-8859-1?q?InZ0nzt9jD1Lpwh7tgsr8VC/xyAV0m1T6xHSxjCbqcF0FxSP+BX3Xe9LlT3f?= =?iso-8859-1?q?P898SIB4JQYC7mYHLHk4DoCJoPyqf7oEUW9Rk717blnaiBozFSXzwoFnhrV2?= =?iso-8859-1?q?XSfqdX4q5BHEXYBRROpcKR9UJI5qAXrcNXB6kWJE8rnVEBZCuD4qFFnW8+Tb?= =?iso-8859-1?q?2uWYsdpNS7Pv8KhnuZUT5UeDBTTS1k4hp8Wpd+wvbb5fL1UYYlYQ=3D?= MIME-Version: 1.0 X-OriginatorOrg: fb.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: SJ0PR15MB5154.namprd15.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7a47d0c7-e480-4f9b-31bc-08da06d14716 X-MS-Exchange-CrossTenant-originalarrivaltime: 15 Mar 2022 22:15:12.1317 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: LvqAgbEvTgV6iio+05pPsjGX+LJljo7hwzBuNdylWfJr1Fo43XZS5uFdTtGcx6at X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR15MB2695 X-Proofpoint-GUID: cTcUTFj4F8oxTnSm1Dwylth2aMRRypYi X-Proofpoint-ORIG-GUID: cTcUTFj4F8oxTnSm1Dwylth2aMRRypYi X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.850,Hydra:6.0.425,FMLib:17.11.64.514 definitions=2022-03-15_11,2022-03-15_01,2022-02-23_01 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net In symmetry with bpf_object__open_skeleton(), bpf_object__open_subskeleton() performs the actual walking and linking of maps, progs, and globals described by bpf_*_skeleton objects. Signed-off-by: Delyan Kratunov --- tools/lib/bpf/libbpf.c | 136 +++++++++++++++++++++++++++++++++------ tools/lib/bpf/libbpf.h | 29 +++++++++ tools/lib/bpf/libbpf.map | 2 + 3 files changed, 146 insertions(+), 21 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index e98a8381aad8..dac905171aaf 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -11812,6 +11812,49 @@ int libbpf_num_possible_cpus(void) return tmp_cpus; } +static int populate_skeleton_maps(const struct bpf_object *obj, + struct bpf_map_skeleton *maps, + size_t map_cnt) +{ + int i; + + for (i = 0; i < map_cnt; i++) { + struct bpf_map **map = maps[i].map; + const char *name = maps[i].name; + void **mmaped = maps[i].mmaped; + + *map = bpf_object__find_map_by_name(obj, name); + if (!*map) { + pr_warn("failed to find skeleton map '%s'\n", name); + return -ESRCH; + } + + /* externs shouldn't be pre-setup from user code */ + if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG) + *mmaped = (*map)->mmaped; + } + return 0; +} + +static int populate_skeleton_progs(const struct bpf_object *obj, + struct bpf_prog_skeleton *progs, + size_t prog_cnt) +{ + int i; + + for (i = 0; i < prog_cnt; i++) { + struct bpf_program **prog = progs[i].prog; + const char *name = progs[i].name; + + *prog = bpf_object__find_program_by_name(obj, name); + if (!*prog) { + pr_warn("failed to find skeleton program '%s'\n", name); + return -ESRCH; + } + } + return 0; +} + int bpf_object__open_skeleton(struct bpf_object_skeleton *s, const struct bpf_object_open_opts *opts) { @@ -11819,7 +11862,7 @@ int bpf_object__open_skeleton(struct bpf_object_skeleton *s, .object_name = s->name, ); struct bpf_object *obj; - int i, err; + int err; /* Attempt to preserve opts->object_name, unless overriden by user * explicitly. Overwriting object name for skeletons is discouraged, @@ -11842,37 +11885,88 @@ int bpf_object__open_skeleton(struct bpf_object_skeleton *s, } *s->obj = obj; + err = populate_skeleton_maps(obj, s->maps, s->map_cnt); + if (err) { + pr_warn("failed to populate skeleton maps for '%s': %d\n", s->name, err); + return libbpf_err(err); + } - for (i = 0; i < s->map_cnt; i++) { - struct bpf_map **map = s->maps[i].map; - const char *name = s->maps[i].name; - void **mmaped = s->maps[i].mmaped; + err = populate_skeleton_progs(obj, s->progs, s->prog_cnt); + if (err) { + pr_warn("failed to populate skeleton progs for '%s': %d\n", s->name, err); + return libbpf_err(err); + } - *map = bpf_object__find_map_by_name(obj, name); - if (!*map) { - pr_warn("failed to find skeleton map '%s'\n", name); - return libbpf_err(-ESRCH); - } + return 0; +} - /* externs shouldn't be pre-setup from user code */ - if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG) - *mmaped = (*map)->mmaped; +int bpf_object__open_subskeleton(struct bpf_object_subskeleton *s) +{ + int err, len, var_idx, i; + const char *var_name; + const struct bpf_map *map; + struct btf *btf; + __u32 map_type_id; + const struct btf_type *map_type, *var_type; + const struct bpf_var_skeleton *var_skel; + struct btf_var_secinfo *var; + + if (!s->obj) + return libbpf_err(-EINVAL); + + btf = bpf_object__btf(s->obj); + if (!btf) + return libbpf_err(-errno); + + err = populate_skeleton_maps(s->obj, s->maps, s->map_cnt); + if (err) { + pr_warn("failed to populate subskeleton maps: %d\n", err); + return libbpf_err(err); } - for (i = 0; i < s->prog_cnt; i++) { - struct bpf_program **prog = s->progs[i].prog; - const char *name = s->progs[i].name; + err = populate_skeleton_progs(s->obj, s->progs, s->prog_cnt); + if (err) { + pr_warn("failed to populate subskeleton maps: %d\n", err); + return libbpf_err(err); + } - *prog = bpf_object__find_program_by_name(obj, name); - if (!*prog) { - pr_warn("failed to find skeleton program '%s'\n", name); - return libbpf_err(-ESRCH); + for (var_idx = 0; var_idx < s->var_cnt; var_idx++) { + var_skel = &s->vars[var_idx]; + map = *var_skel->map; + map_type_id = bpf_map__btf_value_type_id(map); + map_type = btf__type_by_id(btf, map_type_id); + + if (!btf_is_datasec(map_type)) { + pr_warn("Type for map '%1$s' is not a datasec: %2$s", + bpf_map__name(map), + __btf_kind_str(btf_kind(map_type))); + return libbpf_err(-EINVAL); } - } + len = btf_vlen(map_type); + var = btf_var_secinfos(map_type); + for (i = 0; i < len; i++, var++) { + var_type = btf__type_by_id(btf, var->type); + var_name = btf__name_by_offset(btf, var_type->name_off); + if (strcmp(var_name, var_skel->name) == 0) { + *var_skel->addr = map->mmaped + var->offset; + break; + } + } + } return 0; } +void bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s) +{ + if (!s) + return; + free(s->maps); + free(s->progs); + free(s->vars); + free(s); +} + int bpf_object__load_skeleton(struct bpf_object_skeleton *s) { int i, err; diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index c1b0c2ef14d8..1bff7647d797 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -1289,6 +1289,35 @@ LIBBPF_API int bpf_object__attach_skeleton(struct bpf_object_skeleton *s); LIBBPF_API void bpf_object__detach_skeleton(struct bpf_object_skeleton *s); LIBBPF_API void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s); +struct bpf_var_skeleton { + const char *name; + struct bpf_map **map; + void **addr; +}; + +struct bpf_object_subskeleton { + size_t sz; /* size of this struct, for forward/backward compatibility */ + + const struct bpf_object *obj; + + int map_cnt; + int map_skel_sz; /* sizeof(struct bpf_map_skeleton) */ + struct bpf_map_skeleton *maps; + + int prog_cnt; + int prog_skel_sz; /* sizeof(struct bpf_prog_skeleton) */ + struct bpf_prog_skeleton *progs; + + int var_cnt; + int var_skel_sz; /* sizeof(struct bpf_var_skeleton) */ + struct bpf_var_skeleton *vars; +}; + +LIBBPF_API int +bpf_object__open_subskeleton(struct bpf_object_subskeleton *s); +LIBBPF_API void +bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s); + struct gen_loader_opts { size_t sz; /* size of this struct, for forward/backward compatiblity */ const char *data; diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index df1b947792c8..087c77e520e1 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -442,6 +442,8 @@ LIBBPF_0.7.0 { LIBBPF_0.8.0 { global: + bpf_object__destroy_subskeleton; + bpf_object__open_subskeleton; libbpf_register_prog_handler; libbpf_unregister_prog_handler; } LIBBPF_0.7.0;