Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp3418585ybn; Fri, 27 Sep 2019 06:10:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqwflnplCoOiYg+/hknHIvSj6hN9L2y9HOLIx7TRETgJbr+0lc6kkQ+Gzp5po2/JVRU9FmzB X-Received: by 2002:a17:906:fd1:: with SMTP id c17mr7646167ejk.45.1569589832056; Fri, 27 Sep 2019 06:10:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569589832; cv=none; d=google.com; s=arc-20160816; b=rZNpMjg2qK/d15pCBXzFuQKfZrRvmICrQT43L/FnhYesK0MO6zJGGtOGi+pFX2BMqN xp0xOKVvWrzBxSdLLCy0MqBoaeMfPS/pbmafik2c4wD/cSUKUuq8LR/b2iIAq8H0KOUo A3YJKnfeT9vI+HyCEpYyDYkJ/ESLwAPVm4iVNJEQaZThzqKqab1ybL0vsnfTqypd7T6M yqT+CISjxBHU9td0oGLny+tGTyEWm3/Tln8OQ3R9MwWCWipOcrdG75fcQV6Aqs/sFmsp 1S0Vw+XmUIVpit6d8NRYVdCUNYW1yBnoLN5z/l0UGo/8XkITZsp6cyyMY5WWzLQEgYKP PDjA== 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=t798kdQUcfAYE/1V829ZixveOdeFQG/DTCItAoKM464=; b=HrwZnrIiUKoKgsPFXTLLgoIAHRyCpqIV/Q5o0m54HdvEks2HGWIjDVzd/WfpgAPlOg 5tVbzP6BZUYn/k6sNxO7IEWI716ANQ38bu8xj2J7Mp5KOo6ueYwpsD8S+WwvbjMYnH1r ZI2L6YD+iWhfeK/l8bl8bk4YHN/y4/DGkEZzIv5dkxehYLmLTd8yD3o2qEqfLiKTvszt 5Hh917MUDCdg4iUIEDIfkFtfTE2/0znPQP4o+MtLy4OA1iiBjkVEgzDzOM/rf3lcp50i 1SAQ6RA+lwHcIqD6twSfWLhuXk6RMe46RQCds07VcF9YacV/D0sRP8auSC+9PvaWzfWN ea8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=hEQoZA6o; 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=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w18si1486940eda.445.2019.09.27.06.10.07; Fri, 27 Sep 2019 06:10:32 -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=@chromium.org header.s=google header.b=hEQoZA6o; 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=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727445AbfI0NIv (ORCPT + 99 others); Fri, 27 Sep 2019 09:08:51 -0400 Received: from mail-ed1-f68.google.com ([209.85.208.68]:36191 "EHLO mail-ed1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726144AbfI0NIv (ORCPT ); Fri, 27 Sep 2019 09:08:51 -0400 Received: by mail-ed1-f68.google.com with SMTP id h2so2287725edn.3 for ; Fri, 27 Sep 2019 06:08:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=t798kdQUcfAYE/1V829ZixveOdeFQG/DTCItAoKM464=; b=hEQoZA6oVCySqYsY84lMoejfdPlNCU6Tyqp7nB7qv+lkAPFbm+kumHNEMsMrvHueRA NZa1WZXKEJfxfif3wtPhRljiZiRinlKfEOflojVnW6/fj9CrN+nTgcVZNsQswpiJ5eh0 s0gLBo0j3th/2gQl+HUuKFNUeKRSy/NGxA+kc= 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=t798kdQUcfAYE/1V829ZixveOdeFQG/DTCItAoKM464=; b=N40ED7xY+p+LuwO0zkQLvJtv59MeZ4XKXIVEredub2SzLhhtra41qhjf/QVCWt+VV4 l0E4zscybeLp0xqlMFk2EsxN8TxlKTFNFxlFNqQh4PL9GrAVxqAeWXm2yvAgwTmOcB0j yUMURKKtJz06+dBJR6C1n6wS0iPeAOlWaaKyn7QXR+wOzi5plXs2JWt0maY8dq7yPWDE rCIaOJyemdd2mPsI5AgIsWsvDvSHN2JClXwYKEi3L0Qhz9udoWFLY58vaJr+ftqOWcuT bS33H96sBJ+/vtaqbw5SpEobQD7PgZBHEWQB15IbR/GJ5ait/SuB8hD4GnJ/MU5eVTxP QnTQ== X-Gm-Message-State: APjAAAUGXxZo/apV7WIs18435E+xnxZAGwUbAc6rKMOUEpaGDwn4N/RP rOeMBySRTSVQfasQUbPxzkxufI7u5gU= X-Received: by 2002:a17:906:d797:: with SMTP id pj23mr7805635ejb.70.1569589728925; Fri, 27 Sep 2019 06:08:48 -0700 (PDT) Received: from kpsingh-kernel.localdomain ([2620:0:105f:fd00:440a:66a5:a253:2909]) by smtp.gmail.com with ESMTPSA id g19sm567727eje.0.2019.09.27.06.08.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2019 06:08:48 -0700 (PDT) From: KP Singh To: linux-kernel@vger.kernel.org, bpf@vger.kernel.org Cc: Anton Protopopov , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Florent Revest Subject: [PATCH] tools: libbpf: Add bpf_object__open_buffer_xattr Date: Fri, 27 Sep 2019 15:08:34 +0200 Message-Id: <20190927130834.18829-1-kpsingh@chromium.org> X-Mailer: git-send-email 2.20.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 From: KP Singh Introduce struct bpf_object_open_buffer_attr and an API function, bpf_object__open_xattr, as the existing API, bpf_object__open_buffer, doesn't provide a way to specify neither the "needs_kver" nor the "flags" parameter to the internal call to the __bpf_object__open which makes it inconvenient for loading BPF objects that do not require a kernel version from a buffer. The flags attribute in the bpf_object_open_buffer_attr is set to MAPS_RELAX_COMPAT when used in bpf_object__open_buffer to maintain backward compatibility as this was added to load objects with non-compat map definitions in: commit c034a177d3c8 ("bpf: bpftool, add flag to allow non-compat map definitions") and bpf_object__open_buffer was called with this flag enabled (as a boolean true value). The existing "bpf_object__open_xattr" cannot be modified to maintain API compatibility. Reported-by: Anton Protopopov Signed-off-by: KP Singh --- tools/lib/bpf/libbpf.c | 39 ++++++++++++++++++++++++++++----------- tools/lib/bpf/libbpf.h | 10 ++++++++++ tools/lib/bpf/libbpf.map | 5 +++++ 3 files changed, 43 insertions(+), 11 deletions(-) This patch is assimilates the feedback from: https://lore.kernel.org/bpf/20190815000330.12044-1-a.s.protopopov@gmail.com/ I have added a "Reported-by:" tag, but please feel free to update to "Co-developed-by" if it's more appropriate from an attribution perspective. diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 2b57d7ea7836..1f1f2e92832b 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -2752,25 +2752,42 @@ struct bpf_object *bpf_object__open(const char *path) return bpf_object__open_xattr(&attr); } -struct bpf_object *bpf_object__open_buffer(void *obj_buf, - size_t obj_buf_sz, - const char *name) +struct bpf_object * +bpf_object__open_buffer_xattr(struct bpf_object_open_buffer_attr *attr) { char tmp_name[64]; /* param validation */ - if (!obj_buf || obj_buf_sz <= 0) - return NULL; + if (!attr || !attr->obj_buf || !(attr->obj_buf_sz <= 0)) + return ERR_PTR(-EINVAL); - if (!name) { + if (!attr->obj_name) { snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx", - (unsigned long)obj_buf, - (unsigned long)obj_buf_sz); - name = tmp_name; + (unsigned long)attr->obj_buf, + (unsigned long)attr->obj_buf_sz); + attr->obj_name = tmp_name; } - pr_debug("loading object '%s' from buffer\n", name); + pr_debug("loading object '%s' from buffer\n", attr->obj_name); + + return __bpf_object__open(attr->obj_name, attr->obj_buf, + attr->obj_buf_sz, + bpf_prog_type__needs_kver(attr->prog_type), + attr->flags); +} + +struct bpf_object *bpf_object__open_buffer(void *obj_buf, + size_t obj_buf_sz, + const char *name) +{ + struct bpf_object_open_buffer_attr attr = { + .obj_name = name, + .obj_buf = obj_buf, + .obj_buf_sz = obj_buf_sz, + .prog_type = BPF_PROG_TYPE_UNSPEC, + .flags = MAPS_RELAX_COMPAT, + }; - return __bpf_object__open(name, obj_buf, obj_buf_sz, true, true); + return bpf_object__open_buffer_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 5cbf459ece0b..ad0f5a263594 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -67,6 +67,14 @@ struct bpf_object_open_attr { enum bpf_prog_type prog_type; }; +struct bpf_object_open_buffer_attr { + const char *obj_name; + void *obj_buf; + size_t obj_buf_sz; + enum bpf_prog_type prog_type; + int flags; +}; + LIBBPF_API struct bpf_object *bpf_object__open(const char *path); LIBBPF_API struct bpf_object * bpf_object__open_xattr(struct bpf_object_open_attr *attr); @@ -75,6 +83,8 @@ struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr, LIBBPF_API struct bpf_object *bpf_object__open_buffer(void *obj_buf, size_t obj_buf_sz, const char *name); +LIBBPF_API struct bpf_object * +bpf_object__open_buffer_xattr(struct bpf_object_open_buffer_attr *attr); int bpf_object__section_size(const struct bpf_object *obj, const char *name, __u32 *size); int bpf_object__variable_offset(const struct bpf_object *obj, const char *name, diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index f9d316e873d8..4d5dcc58c73d 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -184,3 +184,8 @@ LIBBPF_0.0.4 { perf_buffer__new_raw; perf_buffer__poll; } LIBBPF_0.0.3; + +LIBBPF_0.0.5 { + global: + bpf_object__open_buffer_xattr; +} LIBBPF_0.0.4; -- 2.20.1