tools lib bpf: Add missing BPF functions
authorWang Nan <wangnan0@huawei.com>
Sat, 26 Nov 2016 07:03:25 +0000 (07:03 +0000)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 29 Nov 2016 15:09:36 +0000 (12:09 -0300)
Add more BPF map operations to libbpf. Also add bpf_obj_{pin,get}(). They
can be used on not only BPF maps but also BPF programs.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Cc: He Kuang <hekuang@huawei.com>
Cc: Joe Stringer <joe@ovn.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/20161126070354.141764-2-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/lib/bpf/bpf.c
tools/lib/bpf/bpf.h

index 4212ed62235b46bc687929cdc38f1002a452b6c2..8143536b462a54a9d633e455615085b8a842aa01 100644 (file)
@@ -110,3 +110,59 @@ int bpf_map_update_elem(int fd, void *key, void *value,
 
        return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
 }
+
+int bpf_map_lookup_elem(int fd, void *key, void *value)
+{
+       union bpf_attr attr;
+
+       bzero(&attr, sizeof(attr));
+       attr.map_fd = fd;
+       attr.key = ptr_to_u64(key);
+       attr.value = ptr_to_u64(value);
+
+       return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
+}
+
+int bpf_map_delete_elem(int fd, void *key)
+{
+       union bpf_attr attr;
+
+       bzero(&attr, sizeof(attr));
+       attr.map_fd = fd;
+       attr.key = ptr_to_u64(key);
+
+       return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
+}
+
+int bpf_map_get_next_key(int fd, void *key, void *next_key)
+{
+       union bpf_attr attr;
+
+       bzero(&attr, sizeof(attr));
+       attr.map_fd = fd;
+       attr.key = ptr_to_u64(key);
+       attr.next_key = ptr_to_u64(next_key);
+
+       return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
+}
+
+int bpf_obj_pin(int fd, const char *pathname)
+{
+       union bpf_attr attr;
+
+       bzero(&attr, sizeof(attr));
+       attr.pathname = ptr_to_u64((void *)pathname);
+       attr.bpf_fd = fd;
+
+       return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
+}
+
+int bpf_obj_get(const char *pathname)
+{
+       union bpf_attr attr;
+
+       bzero(&attr, sizeof(attr));
+       attr.pathname = ptr_to_u64((void *)pathname);
+
+       return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
+}
index e8ba540874972c4f0c02204a6f28b00782b7673e..253c3dbb06b420a39558512f89b80ce81a719287 100644 (file)
@@ -35,4 +35,11 @@ int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns,
 
 int bpf_map_update_elem(int fd, void *key, void *value,
                        u64 flags);
+
+int bpf_map_lookup_elem(int fd, void *key, void *value);
+int bpf_map_delete_elem(int fd, void *key);
+int bpf_map_get_next_key(int fd, void *key, void *next_key);
+int bpf_obj_pin(int fd, const char *pathname);
+int bpf_obj_get(const char *pathname);
+
 #endif