Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1426067ybl; Wed, 14 Aug 2019 17:06:30 -0700 (PDT) X-Google-Smtp-Source: APXvYqzpbHVKY4mbkldb5gFMkEIWqIPld9V9s3W29o5KSIWXbiBdxTyEZ3sCd4V447XJcPRkk2Ks X-Received: by 2002:a63:9318:: with SMTP id b24mr1387975pge.31.1565827590076; Wed, 14 Aug 2019 17:06:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565827590; cv=none; d=google.com; s=arc-20160816; b=IgZxBbwMCeB5HE3xecDrzLpjxvfPvlfo8otwlTkkeqFQqXd0hR3iLYeOjUB6sxy1O2 mdmHgm6Rid9dWWpjM5qjb8aDIsJWeubJG8k78Akp7mdoNXKJN2qRocbLF4HKuJNBxpae ljcqBgAvwX2DrTh2oVyvUM/VzZDNNuqte6cUFPMhKuQA95dK7IPQCC0hA/05rYEnp6iF MU6cxHOU7N9MBEyaGcQZT2Wbc/sp3aCZ8w5h7BYXJbt8uMAa8qdRin823lCUkobGBp6d xB7zqkMsLw+bZvDGSsAher9PL/sEMQsi1rMl+IpYoiP6RAAtRdRfmVlgnS2uVU1NwC9U cs4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=YPTBaJLD3D/u6walQ8y9NQD1N5lcuts2Kk/49zILziw=; b=CVD1SdpyM4JJLa8zg6czRRESOyFnvRGkv/QcocqJqL6ZxUxpgDpibo4KChREIPtda2 gUjwqqYnMStpW7BzpBoQsMa4Ht1x5bg7bryFtlOkCB4pHIFAjQ57tZ2T5AHuDV+bZYPh 66ii076m2zqiHDsMQkSfsP0MuJbpDf4VrfvjcRv2QXUdURffnln+N6qoJM4QhFEGPUkh igMN/eRkIAjvcooKXSSfcOSsrgRY8kV2ZJm0Mk+fbvsi99RBDt77uXV/EtCmGtwkKEi8 PGTAt0Cad24mjWQTqmM8cIthuMH+j3mDYeJ8wQCiz4DikGkE2f6unQM/FbG497u40b5B JapQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=a9kKTsPE; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l36si749777pgb.292.2019.08.14.17.06.13; Wed, 14 Aug 2019 17:06:30 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=a9kKTsPE; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728766AbfHOAEB (ORCPT + 99 others); Wed, 14 Aug 2019 20:04:01 -0400 Received: from mail-oi1-f193.google.com ([209.85.167.193]:39625 "EHLO mail-oi1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726119AbfHOAEA (ORCPT ); Wed, 14 Aug 2019 20:04:00 -0400 Received: by mail-oi1-f193.google.com with SMTP id 16so628389oiq.6; Wed, 14 Aug 2019 17:03:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=YPTBaJLD3D/u6walQ8y9NQD1N5lcuts2Kk/49zILziw=; b=a9kKTsPERfA4T4/IuV7BQpfXwEmT0OefjpLqFp1KOxPeJV7X0ITbcYyTNHrxcDkP0U cLZ3tX7s4hLtexS5/mGOUhUcZaXLlfOtcOOGNO6AAaEBRLlRZea+EcP42OSlRoIsAeo7 FeiEpZYerJsJk84Y8N3YdYC5/hF9FSYI9oY+CNRIQie4hBt6W8nAZkz3ZXhtLuzSjwn8 tQoKcKz0qAioZ17+nR1UFeopJQDZ2wOF7qmOd8cx5npJy8diDudk9IOX9Gio0M/ITD8H TGwoc8oLl/PMcuUPa5okGq+7kUaXueotRBHcyGQpJke/zqfT8u8SKhXIcZi7jC6VNGMj HPbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=YPTBaJLD3D/u6walQ8y9NQD1N5lcuts2Kk/49zILziw=; b=iF8X9G4tF/REIlbDkNJZA6Rz0q0XfBEHv6BQKAuB+pIqODb8Lb0Zs9LOjdQqkFrMEL 8DP2ZAHUWS9fb08KjApBqlr/B9ehRSV+hdhjD6AN4N1rHZ0GAvIUekGYT04JLQCLdIHo ee7VMEePI4uWlKUsXPfMMR5mKL1RJq+NXUMIAIx6GxYxeq4NDiqTq2zQ60w87Pp/OD18 kvEey6vm88M2Wbp4vJaQyaq0DSHqFe+9RzBh8iQaxNBKlLwRSHHMoqZe5kLYHL+/hIjF UGlLKmGjkynXI5ccXPNt+0HD6ihqRmvXqTYsas2y+aq58rmrM4CnAgpVipu00pDhzBLo Ly9Q== X-Gm-Message-State: APjAAAU/nHkqzGd3+gXXPWslNBq5DogCK8B/+BgOarJLzwmal4gWQ0kw KHDNxwLFWqnOtU53ngTftNU= X-Received: by 2002:a05:6808:2c3:: with SMTP id a3mr333060oid.121.1565827439381; Wed, 14 Aug 2019 17:03:59 -0700 (PDT) Received: from localhost.members.linode.com ([2600:3c00::f03c:91ff:fe99:7fe5]) by smtp.gmail.com with ESMTPSA id k24sm455013oic.29.2019.08.14.17.03.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2019 17:03:58 -0700 (PDT) From: Anton Protopopov To: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Anton Protopopov Subject: [PATCH bpf-next] tools: libbpf: update extended attributes version of bpf_object__open() Date: Thu, 15 Aug 2019 00:03:30 +0000 Message-Id: <20190815000330.12044-1-a.s.protopopov@gmail.com> X-Mailer: git-send-email 2.19.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Update the bpf_object_open_attr structure and corresponding code so that the bpf_object__open_xattr function could be used to open objects from buffers as well as from files. The reason for this change is that the existing bpf_object__open_buffer function doesn't provide a way to specify neither the needs_kver nor flags parameters to the internal call to __bpf_object__open which makes it inconvenient for loading BPF objects which doesn't require a kernel version. Two new fields, obj_buf and obj_buf_sz, were added to the structure, and the file field was union'ed with obj_name so that one can open an object like this: struct bpf_object_open_attr attr = { .obj_name = name, .obj_buf = obj_buf, .obj_buf_sz = obj_buf_sz, .prog_type = BPF_PROG_TYPE_UNSPEC, }; return bpf_object__open_xattr(&attr); while still being able to use the file semantics: struct bpf_object_open_attr attr = { .file = path, .prog_type = BPF_PROG_TYPE_UNSPEC, }; return bpf_object__open_xattr(&attr); Another thing to note is that since the commit c034a177d3c8 ("bpf: bpftool, add flag to allow non-compat map definitions") which introduced a MAPS_RELAX_COMPAT flag to load objects with non-compat map definitions, bpf_object__open_buffer was called with this flag enabled (it was passed as the boolean true value in flags argument to __bpf_object__open). The default behaviour for all open functions is to clear this flag and this patch changes bpf_object__open_buffer to clears this flag. It can be enabled, if needed, by opening an object from buffer using __bpf_object__open_xattr. Signed-off-by: Anton Protopopov --- tools/lib/bpf/libbpf.c | 45 ++++++++++++++++++++++++++---------------- tools/lib/bpf/libbpf.h | 7 ++++++- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 2233f919dd88..7c8054afd901 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -3630,13 +3630,31 @@ __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz, struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr, int flags) { + char tmp_name[64]; + /* param validation */ - if (!attr->file) + if (!attr) return NULL; - pr_debug("loading %s\n", attr->file); + if (attr->obj_buf) { + if (attr->obj_buf_sz <= 0) + return NULL; + if (!attr->file) { + snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx", + (unsigned long)attr->obj_buf, + (unsigned long)attr->obj_buf_sz); + attr->obj_name = tmp_name; + } + pr_debug("loading object '%s' from buffer\n", attr->obj_name); + } else if (!attr->file) { + return NULL; + } else { + attr->obj_buf_sz = 0; - return __bpf_object__open(attr->file, NULL, 0, + pr_debug("loading object file '%s'\n", attr->file); + } + + return __bpf_object__open(attr->file, attr->obj_buf, attr->obj_buf_sz, bpf_prog_type__needs_kver(attr->prog_type), flags); } @@ -3660,21 +3678,14 @@ struct bpf_object *bpf_object__open_buffer(void *obj_buf, size_t obj_buf_sz, const char *name) { - char tmp_name[64]; - - /* param validation */ - if (!obj_buf || obj_buf_sz <= 0) - return NULL; - - if (!name) { - snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx", - (unsigned long)obj_buf, - (unsigned long)obj_buf_sz); - name = tmp_name; - } - pr_debug("loading object '%s' from buffer\n", name); + struct bpf_object_open_attr attr = { + .obj_name = name, + .obj_buf = obj_buf, + .obj_buf_sz = obj_buf_sz, + .prog_type = BPF_PROG_TYPE_UNSPEC, + }; - return __bpf_object__open(name, obj_buf, obj_buf_sz, true, true); + return bpf_object__open_xattr(&attr); } int bpf_object__unload(struct bpf_object *obj) diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index e8f70977d137..634f278578dd 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -63,8 +63,13 @@ LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn); struct bpf_object; struct bpf_object_open_attr { - const char *file; + union { + const char *file; + const char *obj_name; + }; enum bpf_prog_type prog_type; + void *obj_buf; + size_t obj_buf_sz; }; LIBBPF_API struct bpf_object *bpf_object__open(const char *path); -- 2.19.1