libbpf: Add libbpf support to batch ops
authorYonghong Song <yhs@fb.com>
Wed, 15 Jan 2020 18:43:06 +0000 (10:43 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 15 Jan 2020 22:00:35 +0000 (14:00 -0800)
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 <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200115184308.162644-8-brianvv@google.com
tools/lib/bpf/bpf.c
tools/lib/bpf/bpf.h
tools/lib/bpf/libbpf.map

index 500afe478e94a0b20df3f2fb342a4e5c6fb0d9dc..317727d6121494ecd4512254c3dde8b2ee96a3ba 100644 (file)
@@ -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;
index 56341d117e5baef040d8bf0979703371df20ef3f..b976e77316ccac33a506fed237a0d8bbdf6a4341 100644 (file)
@@ -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);
 
index a19f04e6e3d96084786c305bff31b49402305640..1902a0fc6afcc6985dba0769217366c4deb62248 100644 (file)
@@ -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;