History

samples, bpf: Refactor pointer error check with libbpf

Current method of checking pointer error is not user friendly. Especially the __must_check define makes this less intuitive. Since, libbpf has an API libbpf_get_error() which checks pointer error, this commit refactors existing pointer error check logic with libbpf.

Daniel T. Lee committed 6 years ago

Commit 0efdcef1 parent 96586dd3 files · +6 −15Browse files

Unified · Split

3 files
  1. samples/bpf/sampleip_user.c +2 −5
  2. samples/bpf/trace_event_user.c +3 −6
  3. samples/bpf/xdp_redirect_cpu_user.c +1 −4
samples/bpf/sampleip_user.c+2−5
@@ -18,9 +18,6 @@ · 17 unmodified lines
1818 #include "perf-sys.h"
1919 #include "trace_helpers.h"
2020
21−#define __must_check
22−#include <linux/err.h>
23−
2421 #define DEFAULT_FREQ 99
2522 #define DEFAULT_SECS 5
2623 #define MAX_IPS 8192
@@ -57,7 +54,7 @@ static int sampling_start(int freq, struct bpf_program *prog, · 30 unmodified lines
5754 return 1;
5855 }
5956 links[i] = bpf_program__attach_perf_event(prog, pmu_fd);
60− if (IS_ERR(links[i])) {
57+ if (libbpf_get_error(links[i])) {
6158 fprintf(stderr, "ERROR: Attach perf event\n");
6259 links[i] = NULL;
6360 close(pmu_fd);
@@ -182,7 +179,7 @@ int main(int argc, char **argv) · 118 unmodified lines
182179
183180 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
184181 obj = bpf_object__open_file(filename, NULL);
185− if (IS_ERR(obj)) {
182+ if (libbpf_get_error(obj)) {
186183 fprintf(stderr, "ERROR: opening BPF object file failed\n");
187184 obj = NULL;
188185 goto cleanup;
samples/bpf/trace_event_user.c+3−6
@@ -16,9 +16,6 @@ · 15 unmodified lines
1616 #include "perf-sys.h"
1717 #include "trace_helpers.h"
1818
19−#define __must_check
20−#include <linux/err.h>
21−
2219 #define SAMPLE_FREQ 50
2320
2421 static int pid;
@@ -159,7 +156,7 @@ static void test_perf_event_all_cpu(struct perf_event_attr *attr) · 134 unmodified lines
159156 goto all_cpu_err;
160157 }
161158 links[i] = bpf_program__attach_perf_event(prog, pmu_fd);
162− if (IS_ERR(links[i])) {
159+ if (libbpf_get_error(links[i])) {
163160 printf("bpf_program__attach_perf_event failed\n");
164161 links[i] = NULL;
165162 close(pmu_fd);
@@ -198,7 +195,7 @@ static void test_perf_event_task(struct perf_event_attr *attr) · 32 unmodified lines
198195 goto err;
199196 }
200197 link = bpf_program__attach_perf_event(prog, pmu_fd);
201− if (IS_ERR(link)) {
198+ if (libbpf_get_error(link)) {
202199 printf("bpf_program__attach_perf_event failed\n");
203200 link = NULL;
204201 close(pmu_fd);
@@ -314,7 +311,7 @@ int main(int argc, char **argv) · 109 unmodified lines
314311 }
315312
316313 obj = bpf_object__open_file(filename, NULL);
317− if (IS_ERR(obj)) {
314+ if (libbpf_get_error(obj)) {
318315 printf("opening BPF object file failed\n");
319316 obj = NULL;
320317 goto cleanup;
samples/bpf/xdp_redirect_cpu_user.c+1−4
@@ -19,9 +19,6 @@ static const char *__doc__ = · 18 unmodified lines
1919 #include <time.h>
2020 #include <linux/limits.h>
2121
22−#define __must_check
23−#include <linux/err.h>
24−
2522 #include <arpa/inet.h>
2623 #include <linux/if_link.h>
2724
@@ -622,7 +619,7 @@ static struct bpf_link * attach_tp(struct bpf_object *obj, · 594 unmodified lines
622619 }
623620
624621 link = bpf_program__attach_tracepoint(prog, tp_category, tp_name);
625− if (IS_ERR(link))
622+ if (libbpf_get_error(link))
626623 exit(EXIT_FAIL_BPF);
627624
628625 return link;