diff mbox

[net-next,2/2] tools lib bpf: export function to set type

Message ID 1471090643-22403-3-git-send-email-eric@regit.org
State Changes Requested, archived
Delegated to: David Miller
Headers show

Commit Message

Eric Leblond Aug. 13, 2016, 12:17 p.m. UTC
Current API was not allowing the user to set a type like socket
filter. To avoid a setter function for each type, the patch simply
exports a set function that takes the type in parameter.

Signed-off-by: Eric Leblond <eric@regit.org>
---
 tools/lib/bpf/libbpf.c | 15 ++++++---------
 tools/lib/bpf/libbpf.h |  3 +++
 2 files changed, 9 insertions(+), 9 deletions(-)

Comments

Wangnan (F) Aug. 15, 2016, 3:41 a.m. UTC | #1
On 2016/8/13 20:17, Eric Leblond wrote:
> Current API was not allowing the user to set a type like socket
> filter. To avoid a setter function for each type, the patch simply
> exports a set function that takes the type in parameter.
>
> Signed-off-by: Eric Leblond <eric@regit.org>
> ---
>   tools/lib/bpf/libbpf.c | 15 ++++++---------
>   tools/lib/bpf/libbpf.h |  3 +++
>   2 files changed, 9 insertions(+), 9 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 7872ff6..ff2a8c6 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -1336,26 +1336,23 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n)
>   	return fd;
>   }
>   
> -static void bpf_program__set_type(struct bpf_program *prog,
> +int bpf_program__set_type(struct bpf_program *prog,
>   				  enum bpf_prog_type type)
>   {
> +	if (!prog)
> +		return -EINVAL;
>   	prog->type = type;
> +	return 0;
>   }
>   

We'd better add a sanity check here to prevent setting invalid
program type. Plase have a look at

https://lkml.org/lkml/2015/12/17/13

>   int bpf_program__set_tracepoint(struct bpf_program *prog)
>   {
> -	if (!prog)
> -		return -EINVAL;
> -	bpf_program__set_type(prog, BPF_PROG_TYPE_TRACEPOINT);
> -	return 0;
> +	return bpf_program__set_type(prog, BPF_PROG_TYPE_TRACEPOINT);
>   }
>   
>   int bpf_program__set_kprobe(struct bpf_program *prog)
>   {
> -	if (!prog)
> -		return -EINVAL;
> -	bpf_program__set_type(prog, BPF_PROG_TYPE_KPROBE);
> -	return 0;
> +	return bpf_program__set_type(prog, BPF_PROG_TYPE_KPROBE);
>   }
>   
>   static bool bpf_program__is_type(struct bpf_program *prog,
> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> index a6c5cde..6a84d7a 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -173,6 +173,9 @@ int bpf_program__set_kprobe(struct bpf_program *prog);
>   bool bpf_program__is_tracepoint(struct bpf_program *prog);
>   bool bpf_program__is_kprobe(struct bpf_program *prog);
>   
> +int bpf_program__set_type(struct bpf_program *prog,
> +				  enum bpf_prog_type type);
> +

This makes libbpf.h depend on kernel's uapi/linux/bpf.h.
Although I did this in the above patch, I don't like this
dependency. This is the reason why I introduce

int bpf_program__set_tracepoint(struct bpf_program *prog);
int bpf_program__set_kprobe(struct bpf_program *prog);

bool bpf_program__is_tracepoint(struct bpf_program *prog);
bool bpf_program__is_kprobe(struct bpf_program *prog);

and hide bpf_program__set_type inside the .c file.

Please add type setting functions, or pass int value to
bpf_program__set_type and add sanity checking.

Thank you.
diff mbox

Patch

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 7872ff6..ff2a8c6 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -1336,26 +1336,23 @@  int bpf_program__nth_fd(struct bpf_program *prog, int n)
 	return fd;
 }
 
-static void bpf_program__set_type(struct bpf_program *prog,
+int bpf_program__set_type(struct bpf_program *prog,
 				  enum bpf_prog_type type)
 {
+	if (!prog)
+		return -EINVAL;
 	prog->type = type;
+	return 0;
 }
 
 int bpf_program__set_tracepoint(struct bpf_program *prog)
 {
-	if (!prog)
-		return -EINVAL;
-	bpf_program__set_type(prog, BPF_PROG_TYPE_TRACEPOINT);
-	return 0;
+	return bpf_program__set_type(prog, BPF_PROG_TYPE_TRACEPOINT);
 }
 
 int bpf_program__set_kprobe(struct bpf_program *prog)
 {
-	if (!prog)
-		return -EINVAL;
-	bpf_program__set_type(prog, BPF_PROG_TYPE_KPROBE);
-	return 0;
+	return bpf_program__set_type(prog, BPF_PROG_TYPE_KPROBE);
 }
 
 static bool bpf_program__is_type(struct bpf_program *prog,
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index a6c5cde..6a84d7a 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -173,6 +173,9 @@  int bpf_program__set_kprobe(struct bpf_program *prog);
 bool bpf_program__is_tracepoint(struct bpf_program *prog);
 bool bpf_program__is_kprobe(struct bpf_program *prog);
 
+int bpf_program__set_type(struct bpf_program *prog,
+				  enum bpf_prog_type type);
+
 /*
  * We don't need __attribute__((packed)) now since it is
  * unnecessary for 'bpf_map_def' because they are all aligned.