Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp6823628ybl; Wed, 15 Jan 2020 10:45:07 -0800 (PST) X-Google-Smtp-Source: APXvYqwVf4+/I146O+2UbFe+hWoBtBQEp3HJn30BUKPNDF4tx/Wb/+oZnH427mMbtoC05gKiXj1I X-Received: by 2002:a05:6830:2099:: with SMTP id y25mr3579403otq.87.1579113906901; Wed, 15 Jan 2020 10:45:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579113906; cv=none; d=google.com; s=arc-20160816; b=IarbRoGK11F/MySAGXvp1YdU8hzaN1HG9JiyOW1kHh3zIEQVRadzTZjqXVLhupoxY1 o7jeHskAqKSmdUO95WPNJqymGu/kDBYwdG2ajg39btFo21FrZOzUoUlL0NLrkxRJF+Zi DfMEdUcYugHppfr5iLpUFsA/K1lSwOb9qtHEjqomkJxrSpXpOf7DG+36zNbox524WcdT Y6M7bsUkBWNuGoEEKxg8hMNHg8KihyJpPPNJrRKAznWsF2ylEGcHCqThZJpudGPtpNJI c7ijlIf9fzXsG2K/663j4GVQ0vOP4mczYm4KPeDMU0slnvRLCmr1Bd8pO4cgt6r39BX5 ylEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=RCyrkp6zT5EPgtnS5mYgbeYBhIOvjRmdaqV1JP6GZIM=; b=oyoqMbf29qms0NWYT2ezXyCrG6oF9a33nGHKhHreah5cnOjRfb6Oozrye4qWyWsXQt UC1H7Y4cfXSsk0hTbin3R2fBXRyrywZcpMwvTR6X8991ofhBtfVYJpaIIXxA13C6yNZg lU3pSElgJAi+Yn7FqoOow89VLrZWgxS0s9fW3Sfyti5NtHRn8UvzfC8KZW+7nIr/0KEy 5YKM6LarSRVXkGpKiDGfNaYG1VlNZZFaMiZwfglcFdfPX51nu8M49C05ylgmSdNBFu48 MlbmvcwuNkV01ylVIs2xpArV9WbLoNq3RAgjc7lDUyeq5FzK5CFtG4wR1edjD/+/9kuh 6jRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=BKVmoDfc; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 6si9724861oin.93.2020.01.15.10.44.54; Wed, 15 Jan 2020 10:45:06 -0800 (PST) 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=@google.com header.s=20161025 header.b=BKVmoDfc; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729467AbgAOSnq (ORCPT + 99 others); Wed, 15 Jan 2020 13:43:46 -0500 Received: from mail-pf1-f202.google.com ([209.85.210.202]:32857 "EHLO mail-pf1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729406AbgAOSnj (ORCPT ); Wed, 15 Jan 2020 13:43:39 -0500 Received: by mail-pf1-f202.google.com with SMTP id c72so11421863pfc.0 for ; Wed, 15 Jan 2020 10:43:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=RCyrkp6zT5EPgtnS5mYgbeYBhIOvjRmdaqV1JP6GZIM=; b=BKVmoDfcMdak267QSTOLD7Kr7mys7DR6FMqoEiJoy7U2bcelFMYINew1NNR4zVak31 1i6qmI3np3uqMC+7PEJQ6MzpuUDdRc35ffj1NQIp5imJ8flKY6SahNdy07V/tr+Ohsd8 gwd1pTlMOero2dI3lesFWGgb+JTkeQLlYBqB4sAAeNRdjur4PmVjFzd/Cs4w/h9SwnJx x0lJR3yiR2n+MOzKWMnuTsmWiZvDGjW7QvUUQkJ2xs8TPH61M5qe9bE2Ms/pAvsSLfVR XZRsFbf6YL02n0R6TYFFBePCYEGjBFV7BL2O6wSuvMR4eC8PJc3+Q3eRxORDxj7Ojuoc j9aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=RCyrkp6zT5EPgtnS5mYgbeYBhIOvjRmdaqV1JP6GZIM=; b=Cm/ebPS+Vq4FdAhP+/htUNODiElt0wRU8ZoRpT8EcVmm6OGUWzOmbQsNRG4/bvZUfA taIkkchcZsU2EUbSFvlkuN7+Ffk5Hk3wOF1gbsemWLgyEEdw91tnworB0/9SMg20kMXw E3QuvpmSGzLsmSe9BYlgdk2XMB2TTJw28iKq+3QYHzgciJPWdqVEPxNII0pUVA7tI6ys etta6WhfdSWrHLpCmnxzsjwnEXSoQznkx67jQkiGUQvHv6DhTF9RGGOYQIQT8Iwg0Em+ y3zeUjc9YpzA7TwKr2+3VBfFQXKNKv0cakYe7Rfuk3swBMKn18DpCAYbrc3AYPZF/BxU aehA== X-Gm-Message-State: APjAAAUhBB4zZFkoS9YdsQq4MVkowheFciR4nqEaInUfOqHxkK2AZVPW 7/LNDSqBiL5qY116Sov0SYxTHyfsD+8c X-Received: by 2002:a63:d20a:: with SMTP id a10mr33681152pgg.273.1579113819263; Wed, 15 Jan 2020 10:43:39 -0800 (PST) Date: Wed, 15 Jan 2020 10:43:06 -0800 In-Reply-To: <20200115184308.162644-1-brianvv@google.com> Message-Id: <20200115184308.162644-8-brianvv@google.com> Mime-Version: 1.0 References: <20200115184308.162644-1-brianvv@google.com> X-Mailer: git-send-email 2.25.0.rc1.283.g88dfdc4193-goog Subject: [PATCH v5 bpf-next 7/9] libbpf: add libbpf support to batch ops From: Brian Vazquez To: Brian Vazquez , Brian Vazquez , Alexei Starovoitov , Daniel Borkmann , "David S . Miller" Cc: Yonghong Song , Andrii Nakryiko , Stanislav Fomichev , Petar Penkov , Willem de Bruijn , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yonghong Song Added four libbpf API functions to support map batch operations: . int bpf_map_delete_batch( ... ) . int bpf_map_lookup_batch( ... ) . int bpf_map_lookup_and_delete_batch( ... ) . int bpf_map_update_batch( ... ) Signed-off-by: Yonghong Song --- tools/lib/bpf/bpf.c | 58 ++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/bpf.h | 22 +++++++++++++++ tools/lib/bpf/libbpf.map | 4 +++ 3 files changed, 84 insertions(+) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 500afe478e94a..317727d612149 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -452,6 +452,64 @@ int bpf_map_freeze(int fd) return sys_bpf(BPF_MAP_FREEZE, &attr, sizeof(attr)); } +static int bpf_map_batch_common(int cmd, int fd, void *in_batch, + void *out_batch, void *keys, void *values, + __u32 *count, + const struct bpf_map_batch_opts *opts) +{ + union bpf_attr attr = {}; + int ret; + + if (!OPTS_VALID(opts, bpf_map_batch_opts)) + return -EINVAL; + + memset(&attr, 0, sizeof(attr)); + attr.batch.map_fd = fd; + attr.batch.in_batch = ptr_to_u64(in_batch); + attr.batch.out_batch = ptr_to_u64(out_batch); + attr.batch.keys = ptr_to_u64(keys); + attr.batch.values = ptr_to_u64(values); + attr.batch.count = *count; + attr.batch.elem_flags = OPTS_GET(opts, elem_flags, 0); + attr.batch.flags = OPTS_GET(opts, flags, 0); + + ret = sys_bpf(cmd, &attr, sizeof(attr)); + *count = attr.batch.count; + + return ret; +} + +int bpf_map_delete_batch(int fd, void *keys, __u32 *count, + const struct bpf_map_batch_opts *opts) +{ + return bpf_map_batch_common(BPF_MAP_DELETE_BATCH, fd, NULL, + NULL, keys, NULL, count, opts); +} + +int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch, void *keys, + void *values, __u32 *count, + const struct bpf_map_batch_opts *opts) +{ + return bpf_map_batch_common(BPF_MAP_LOOKUP_BATCH, fd, in_batch, + out_batch, keys, values, count, opts); +} + +int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, void *out_batch, + void *keys, void *values, __u32 *count, + const struct bpf_map_batch_opts *opts) +{ + return bpf_map_batch_common(BPF_MAP_LOOKUP_AND_DELETE_BATCH, + fd, in_batch, out_batch, keys, values, + count, opts); +} + +int bpf_map_update_batch(int fd, void *keys, void *values, __u32 *count, + const struct bpf_map_batch_opts *opts) +{ + return bpf_map_batch_common(BPF_MAP_UPDATE_BATCH, fd, NULL, NULL, + keys, values, count, opts); +} + int bpf_obj_pin(int fd, const char *pathname) { union bpf_attr attr; diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 56341d117e5ba..b976e77316cca 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -127,6 +127,28 @@ LIBBPF_API int bpf_map_lookup_and_delete_elem(int fd, const void *key, LIBBPF_API int bpf_map_delete_elem(int fd, const void *key); LIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key); LIBBPF_API int bpf_map_freeze(int fd); + +struct bpf_map_batch_opts { + size_t sz; /* size of this struct for forward/backward compatibility */ + __u64 elem_flags; + __u64 flags; +}; +#define bpf_map_batch_opts__last_field flags + +LIBBPF_API int bpf_map_delete_batch(int fd, void *keys, + __u32 *count, + const struct bpf_map_batch_opts *opts); +LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch, + void *keys, void *values, __u32 *count, + const struct bpf_map_batch_opts *opts); +LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch, + void *out_batch, void *keys, + void *values, __u32 *count, + const struct bpf_map_batch_opts *opts); +LIBBPF_API int bpf_map_update_batch(int fd, void *keys, void *values, + __u32 *count, + const struct bpf_map_batch_opts *opts); + LIBBPF_API int bpf_obj_pin(int fd, const char *pathname); LIBBPF_API int bpf_obj_get(const char *pathname); diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index a19f04e6e3d96..1902a0fc6afcc 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -214,6 +214,10 @@ LIBBPF_0.0.7 { btf_dump__emit_type_decl; bpf_link__disconnect; bpf_map__attach_struct_ops; + bpf_map_delete_batch; + bpf_map_lookup_and_delete_batch; + bpf_map_lookup_batch; + bpf_map_update_batch; bpf_object__find_program_by_name; bpf_object__attach_skeleton; bpf_object__destroy_skeleton; -- 2.25.0.rc1.283.g88dfdc4193-goog