diff mbox

[05/34] tools lib bpf: Add missing bpf map functions

Message ID 20161115040617.69788-6-wangnan0@huawei.com
State Superseded
Headers show

Commit Message

Wang Nan Nov. 15, 2016, 4:05 a.m. UTC
Add more BPF map operations to libbpf.

Signed-off-by: Wang Nan <wangnan0@huawei.com>

Cc: Alexei Starovoitov <ast@fb.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Li Zefan <lizefan@huawei.com>
---
 tools/lib/bpf/bpf.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/bpf.h |  7 +++++++
 2 files changed, 63 insertions(+)

-- 
2.10.1

Comments

Wang Nan Nov. 17, 2016, 3:23 a.m. UTC | #1
On 2016/11/15 12:05, Wang Nan wrote:
> Add more BPF map operations to libbpf.

>

> Signed-off-by: Wang Nan <wangnan0@huawei.com>

> Cc: Alexei Starovoitov <ast@fb.com>

> Cc: Arnaldo Carvalho de Melo <acme@redhat.com>

> Cc: Li Zefan <lizefan@huawei.com>

> ---

>   tools/lib/bpf/bpf.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++

>   tools/lib/bpf/bpf.h |  7 +++++++

>   2 files changed, 63 insertions(+)

>

> diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c

> index 4212ed6..e966248 100644

> --- a/tools/lib/bpf/bpf.c

> +++ b/tools/lib/bpf/bpf.c

> @@ -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_map_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_map_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));

> +}


bpf_map_{pin,get} should be rename to bpf_obj_{pin,get} since they can
be used on BPF program. Thanks to Joe Stringer.

> diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h

> index e8ba540..5b3e52b 100644

> --- a/tools/lib/bpf/bpf.h

> +++ b/tools/lib/bpf/bpf.h

> @@ -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_map_pin(int fd, const char *pathname);

> +int bpf_map_get(const char *pathname);

> +

>   #endif
Arnaldo Carvalho de Melo Nov. 25, 2016, 2:31 p.m. UTC | #2
Em Thu, Nov 17, 2016 at 11:23:58AM +0800, Wangnan (F) escreveu:
> 

> 

> On 2016/11/15 12:05, Wang Nan wrote:

> >Add more BPF map operations to libbpf.

> >

> >Signed-off-by: Wang Nan <wangnan0@huawei.com>

> >Cc: Alexei Starovoitov <ast@fb.com>

> >Cc: Arnaldo Carvalho de Melo <acme@redhat.com>

> >Cc: Li Zefan <lizefan@huawei.com>

> >---

> >  tools/lib/bpf/bpf.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++

> >  tools/lib/bpf/bpf.h |  7 +++++++

> >  2 files changed, 63 insertions(+)

> >

> >diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c

> >index 4212ed6..e966248 100644

> >--- a/tools/lib/bpf/bpf.c

> >+++ b/tools/lib/bpf/bpf.c

> >@@ -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_map_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_map_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));

> >+}

> 

> bpf_map_{pin,get} should be rename to bpf_obj_{pin,get} since they can

> be used on BPF program. Thanks to Joe Stringer.


Ok, and keep:

+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);

as-is?

I'll push te first four patches in this series, will continue after
clarifying this one.

- Arnaldo

 
> >diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h

> >index e8ba540..5b3e52b 100644

> >--- a/tools/lib/bpf/bpf.h

> >+++ b/tools/lib/bpf/bpf.h

> >@@ -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_map_pin(int fd, const char *pathname);

> >+int bpf_map_get(const char *pathname);

> >+

> >  #endif

>
diff mbox

Patch

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 4212ed6..e966248 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -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_map_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_map_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));
+}
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index e8ba540..5b3e52b 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -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_map_pin(int fd, const char *pathname);
+int bpf_map_get(const char *pathname);
+
 #endif