aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--samples/bpf/Makefile8
-rw-r--r--samples/bpf/xdp1_user.c31
-rw-r--r--samples/bpf/xdp_adjust_tail_user.c36
-rw-r--r--samples/bpf/xdp_rxq_info_user.c46
4 files changed, 78 insertions, 43 deletions
diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
index 8ce72d211c3e..9e255ca4059a 100644
--- a/samples/bpf/Makefile
+++ b/samples/bpf/Makefile
@@ -79,9 +79,9 @@ test_cgrp2_attach-objs := test_cgrp2_attach.o $(LIBBPF)
79test_cgrp2_attach2-objs := test_cgrp2_attach2.o $(LIBBPF) $(CGROUP_HELPERS) 79test_cgrp2_attach2-objs := test_cgrp2_attach2.o $(LIBBPF) $(CGROUP_HELPERS)
80test_cgrp2_sock-objs := test_cgrp2_sock.o $(LIBBPF) 80test_cgrp2_sock-objs := test_cgrp2_sock.o $(LIBBPF)
81test_cgrp2_sock2-objs := bpf_load.o $(LIBBPF) test_cgrp2_sock2.o 81test_cgrp2_sock2-objs := bpf_load.o $(LIBBPF) test_cgrp2_sock2.o
82xdp1-objs := bpf_load.o $(LIBBPF) xdp1_user.o 82xdp1-objs := xdp1_user.o $(LIBBPF)
83# reuse xdp1 source intentionally 83# reuse xdp1 source intentionally
84xdp2-objs := bpf_load.o $(LIBBPF) xdp1_user.o 84xdp2-objs := xdp1_user.o $(LIBBPF)
85xdp_router_ipv4-objs := bpf_load.o $(LIBBPF) xdp_router_ipv4_user.o 85xdp_router_ipv4-objs := bpf_load.o $(LIBBPF) xdp_router_ipv4_user.o
86test_current_task_under_cgroup-objs := bpf_load.o $(LIBBPF) $(CGROUP_HELPERS) \ 86test_current_task_under_cgroup-objs := bpf_load.o $(LIBBPF) $(CGROUP_HELPERS) \
87 test_current_task_under_cgroup_user.o 87 test_current_task_under_cgroup_user.o
@@ -96,10 +96,10 @@ xdp_redirect-objs := bpf_load.o $(LIBBPF) xdp_redirect_user.o
96xdp_redirect_map-objs := bpf_load.o $(LIBBPF) xdp_redirect_map_user.o 96xdp_redirect_map-objs := bpf_load.o $(LIBBPF) xdp_redirect_map_user.o
97xdp_redirect_cpu-objs := bpf_load.o $(LIBBPF) xdp_redirect_cpu_user.o 97xdp_redirect_cpu-objs := bpf_load.o $(LIBBPF) xdp_redirect_cpu_user.o
98xdp_monitor-objs := bpf_load.o $(LIBBPF) xdp_monitor_user.o 98xdp_monitor-objs := bpf_load.o $(LIBBPF) xdp_monitor_user.o
99xdp_rxq_info-objs := bpf_load.o $(LIBBPF) xdp_rxq_info_user.o 99xdp_rxq_info-objs := xdp_rxq_info_user.o $(LIBBPF)
100syscall_tp-objs := bpf_load.o $(LIBBPF) syscall_tp_user.o 100syscall_tp-objs := bpf_load.o $(LIBBPF) syscall_tp_user.o
101cpustat-objs := bpf_load.o $(LIBBPF) cpustat_user.o 101cpustat-objs := bpf_load.o $(LIBBPF) cpustat_user.o
102xdp_adjust_tail-objs := bpf_load.o $(LIBBPF) xdp_adjust_tail_user.o 102xdp_adjust_tail-objs := xdp_adjust_tail_user.o $(LIBBPF)
103xdpsock-objs := bpf_load.o $(LIBBPF) xdpsock_user.o 103xdpsock-objs := bpf_load.o $(LIBBPF) xdpsock_user.o
104xdp_fwd-objs := bpf_load.o $(LIBBPF) xdp_fwd_user.o 104xdp_fwd-objs := bpf_load.o $(LIBBPF) xdp_fwd_user.o
105 105
diff --git a/samples/bpf/xdp1_user.c b/samples/bpf/xdp1_user.c
index b901ee2b3336..b02c531510ed 100644
--- a/samples/bpf/xdp1_user.c
+++ b/samples/bpf/xdp1_user.c
@@ -16,9 +16,9 @@
16#include <libgen.h> 16#include <libgen.h>
17#include <sys/resource.h> 17#include <sys/resource.h>
18 18
19#include "bpf_load.h"
20#include "bpf_util.h" 19#include "bpf_util.h"
21#include "libbpf.h" 20#include "bpf/bpf.h"
21#include "bpf/libbpf.h"
22 22
23static int ifindex; 23static int ifindex;
24static __u32 xdp_flags; 24static __u32 xdp_flags;
@@ -31,7 +31,7 @@ static void int_exit(int sig)
31 31
32/* simple per-protocol drop counter 32/* simple per-protocol drop counter
33 */ 33 */
34static void poll_stats(int interval) 34static void poll_stats(int map_fd, int interval)
35{ 35{
36 unsigned int nr_cpus = bpf_num_possible_cpus(); 36 unsigned int nr_cpus = bpf_num_possible_cpus();
37 const unsigned int nr_keys = 256; 37 const unsigned int nr_keys = 256;
@@ -47,7 +47,7 @@ static void poll_stats(int interval)
47 for (key = 0; key < nr_keys; key++) { 47 for (key = 0; key < nr_keys; key++) {
48 __u64 sum = 0; 48 __u64 sum = 0;
49 49
50 assert(bpf_map_lookup_elem(map_fd[0], &key, values) == 0); 50 assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
51 for (i = 0; i < nr_cpus; i++) 51 for (i = 0; i < nr_cpus; i++)
52 sum += (values[i] - prev[key][i]); 52 sum += (values[i] - prev[key][i]);
53 if (sum) 53 if (sum)
@@ -71,9 +71,14 @@ static void usage(const char *prog)
71int main(int argc, char **argv) 71int main(int argc, char **argv)
72{ 72{
73 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; 73 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
74 struct bpf_prog_load_attr prog_load_attr = {
75 .prog_type = BPF_PROG_TYPE_XDP,
76 };
74 const char *optstr = "SN"; 77 const char *optstr = "SN";
78 int prog_fd, map_fd, opt;
79 struct bpf_object *obj;
80 struct bpf_map *map;
75 char filename[256]; 81 char filename[256];
76 int opt;
77 82
78 while ((opt = getopt(argc, argv, optstr)) != -1) { 83 while ((opt = getopt(argc, argv, optstr)) != -1) {
79 switch (opt) { 84 switch (opt) {
@@ -102,13 +107,19 @@ int main(int argc, char **argv)
102 ifindex = strtoul(argv[optind], NULL, 0); 107 ifindex = strtoul(argv[optind], NULL, 0);
103 108
104 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); 109 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
110 prog_load_attr.file = filename;
105 111
106 if (load_bpf_file(filename)) { 112 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
107 printf("%s", bpf_log_buf); 113 return 1;
114
115 map = bpf_map__next(NULL, obj);
116 if (!map) {
117 printf("finding a map in obj file failed\n");
108 return 1; 118 return 1;
109 } 119 }
120 map_fd = bpf_map__fd(map);
110 121
111 if (!prog_fd[0]) { 122 if (!prog_fd) {
112 printf("load_bpf_file: %s\n", strerror(errno)); 123 printf("load_bpf_file: %s\n", strerror(errno));
113 return 1; 124 return 1;
114 } 125 }
@@ -116,12 +127,12 @@ int main(int argc, char **argv)
116 signal(SIGINT, int_exit); 127 signal(SIGINT, int_exit);
117 signal(SIGTERM, int_exit); 128 signal(SIGTERM, int_exit);
118 129
119 if (bpf_set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) { 130 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
120 printf("link set xdp fd failed\n"); 131 printf("link set xdp fd failed\n");
121 return 1; 132 return 1;
122 } 133 }
123 134
124 poll_stats(2); 135 poll_stats(map_fd, 2);
125 136
126 return 0; 137 return 0;
127} 138}
diff --git a/samples/bpf/xdp_adjust_tail_user.c b/samples/bpf/xdp_adjust_tail_user.c
index f621a541b574..3042ce37dae8 100644
--- a/samples/bpf/xdp_adjust_tail_user.c
+++ b/samples/bpf/xdp_adjust_tail_user.c
@@ -18,9 +18,8 @@
18#include <netinet/ether.h> 18#include <netinet/ether.h>
19#include <unistd.h> 19#include <unistd.h>
20#include <time.h> 20#include <time.h>
21#include "bpf_load.h" 21#include "bpf/bpf.h"
22#include "libbpf.h" 22#include "bpf/libbpf.h"
23#include "bpf_util.h"
24 23
25#define STATS_INTERVAL_S 2U 24#define STATS_INTERVAL_S 2U
26 25
@@ -36,7 +35,7 @@ static void int_exit(int sig)
36 35
37/* simple "icmp packet too big sent" counter 36/* simple "icmp packet too big sent" counter
38 */ 37 */
39static void poll_stats(unsigned int kill_after_s) 38static void poll_stats(unsigned int map_fd, unsigned int kill_after_s)
40{ 39{
41 time_t started_at = time(NULL); 40 time_t started_at = time(NULL);
42 __u64 value = 0; 41 __u64 value = 0;
@@ -46,7 +45,7 @@ static void poll_stats(unsigned int kill_after_s)
46 while (!kill_after_s || time(NULL) - started_at <= kill_after_s) { 45 while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
47 sleep(STATS_INTERVAL_S); 46 sleep(STATS_INTERVAL_S);
48 47
49 assert(bpf_map_lookup_elem(map_fd[0], &key, &value) == 0); 48 assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
50 49
51 printf("icmp \"packet too big\" sent: %10llu pkts\n", value); 50 printf("icmp \"packet too big\" sent: %10llu pkts\n", value);
52 } 51 }
@@ -66,14 +65,17 @@ static void usage(const char *cmd)
66 65
67int main(int argc, char **argv) 66int main(int argc, char **argv)
68{ 67{
68 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
69 struct bpf_prog_load_attr prog_load_attr = {
70 .prog_type = BPF_PROG_TYPE_XDP,
71 };
69 unsigned char opt_flags[256] = {}; 72 unsigned char opt_flags[256] = {};
70 unsigned int kill_after_s = 0; 73 unsigned int kill_after_s = 0;
71 const char *optstr = "i:T:SNh"; 74 const char *optstr = "i:T:SNh";
72 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; 75 int i, prog_fd, map_fd, opt;
76 struct bpf_object *obj;
77 struct bpf_map *map;
73 char filename[256]; 78 char filename[256];
74 int opt;
75 int i;
76
77 79
78 for (i = 0; i < strlen(optstr); i++) 80 for (i = 0; i < strlen(optstr); i++)
79 if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z') 81 if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
@@ -115,13 +117,19 @@ int main(int argc, char **argv)
115 } 117 }
116 118
117 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); 119 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
120 prog_load_attr.file = filename;
121
122 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
123 return 1;
118 124
119 if (load_bpf_file(filename)) { 125 map = bpf_map__next(NULL, obj);
120 printf("%s", bpf_log_buf); 126 if (!map) {
127 printf("finding a map in obj file failed\n");
121 return 1; 128 return 1;
122 } 129 }
130 map_fd = bpf_map__fd(map);
123 131
124 if (!prog_fd[0]) { 132 if (!prog_fd) {
125 printf("load_bpf_file: %s\n", strerror(errno)); 133 printf("load_bpf_file: %s\n", strerror(errno));
126 return 1; 134 return 1;
127 } 135 }
@@ -129,12 +137,12 @@ int main(int argc, char **argv)
129 signal(SIGINT, int_exit); 137 signal(SIGINT, int_exit);
130 signal(SIGTERM, int_exit); 138 signal(SIGTERM, int_exit);
131 139
132 if (bpf_set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) { 140 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
133 printf("link set xdp fd failed\n"); 141 printf("link set xdp fd failed\n");
134 return 1; 142 return 1;
135 } 143 }
136 144
137 poll_stats(kill_after_s); 145 poll_stats(map_fd, kill_after_s);
138 146
139 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags); 147 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
140 148
diff --git a/samples/bpf/xdp_rxq_info_user.c b/samples/bpf/xdp_rxq_info_user.c
index 478d95412de4..e4e9ba52bff0 100644
--- a/samples/bpf/xdp_rxq_info_user.c
+++ b/samples/bpf/xdp_rxq_info_user.c
@@ -22,8 +22,8 @@ static const char *__doc__ = " XDP RX-queue info extract example\n\n"
22#include <arpa/inet.h> 22#include <arpa/inet.h>
23#include <linux/if_link.h> 23#include <linux/if_link.h>
24 24
25#include "libbpf.h" 25#include "bpf/bpf.h"
26#include "bpf_load.h" 26#include "bpf/libbpf.h"
27#include "bpf_util.h" 27#include "bpf_util.h"
28 28
29static int ifindex = -1; 29static int ifindex = -1;
@@ -32,6 +32,9 @@ static char *ifname;
32 32
33static __u32 xdp_flags; 33static __u32 xdp_flags;
34 34
35static struct bpf_map *stats_global_map;
36static struct bpf_map *rx_queue_index_map;
37
35/* Exit return codes */ 38/* Exit return codes */
36#define EXIT_OK 0 39#define EXIT_OK 0
37#define EXIT_FAIL 1 40#define EXIT_FAIL 1
@@ -174,7 +177,7 @@ static struct datarec *alloc_record_per_cpu(void)
174 177
175static struct record *alloc_record_per_rxq(void) 178static struct record *alloc_record_per_rxq(void)
176{ 179{
177 unsigned int nr_rxqs = map_data[2].def.max_entries; 180 unsigned int nr_rxqs = bpf_map__def(rx_queue_index_map)->max_entries;
178 struct record *array; 181 struct record *array;
179 size_t size; 182 size_t size;
180 183
@@ -190,7 +193,7 @@ static struct record *alloc_record_per_rxq(void)
190 193
191static struct stats_record *alloc_stats_record(void) 194static struct stats_record *alloc_stats_record(void)
192{ 195{
193 unsigned int nr_rxqs = map_data[2].def.max_entries; 196 unsigned int nr_rxqs = bpf_map__def(rx_queue_index_map)->max_entries;
194 struct stats_record *rec; 197 struct stats_record *rec;
195 int i; 198 int i;
196 199
@@ -210,7 +213,7 @@ static struct stats_record *alloc_stats_record(void)
210 213
211static void free_stats_record(struct stats_record *r) 214static void free_stats_record(struct stats_record *r)
212{ 215{
213 unsigned int nr_rxqs = map_data[2].def.max_entries; 216 unsigned int nr_rxqs = bpf_map__def(rx_queue_index_map)->max_entries;
214 int i; 217 int i;
215 218
216 for (i = 0; i < nr_rxqs; i++) 219 for (i = 0; i < nr_rxqs; i++)
@@ -254,11 +257,11 @@ static void stats_collect(struct stats_record *rec)
254{ 257{
255 int fd, i, max_rxqs; 258 int fd, i, max_rxqs;
256 259
257 fd = map_data[1].fd; /* map: stats_global_map */ 260 fd = bpf_map__fd(stats_global_map);
258 map_collect_percpu(fd, 0, &rec->stats); 261 map_collect_percpu(fd, 0, &rec->stats);
259 262
260 fd = map_data[2].fd; /* map: rx_queue_index_map */ 263 fd = bpf_map__fd(rx_queue_index_map);
261 max_rxqs = map_data[2].def.max_entries; 264 max_rxqs = bpf_map__def(rx_queue_index_map)->max_entries;
262 for (i = 0; i < max_rxqs; i++) 265 for (i = 0; i < max_rxqs; i++)
263 map_collect_percpu(fd, i, &rec->rxq[i]); 266 map_collect_percpu(fd, i, &rec->rxq[i]);
264} 267}
@@ -304,8 +307,8 @@ static void stats_print(struct stats_record *stats_rec,
304 struct stats_record *stats_prev, 307 struct stats_record *stats_prev,
305 int action) 308 int action)
306{ 309{
310 unsigned int nr_rxqs = bpf_map__def(rx_queue_index_map)->max_entries;
307 unsigned int nr_cpus = bpf_num_possible_cpus(); 311 unsigned int nr_cpus = bpf_num_possible_cpus();
308 unsigned int nr_rxqs = map_data[2].def.max_entries;
309 double pps = 0, err = 0; 312 double pps = 0, err = 0;
310 struct record *rec, *prev; 313 struct record *rec, *prev;
311 double t; 314 double t;
@@ -419,31 +422,44 @@ static void stats_poll(int interval, int action)
419int main(int argc, char **argv) 422int main(int argc, char **argv)
420{ 423{
421 struct rlimit r = {10 * 1024 * 1024, RLIM_INFINITY}; 424 struct rlimit r = {10 * 1024 * 1024, RLIM_INFINITY};
425 struct bpf_prog_load_attr prog_load_attr = {
426 .prog_type = BPF_PROG_TYPE_XDP,
427 };
428 int prog_fd, map_fd, opt, err;
422 bool use_separators = true; 429 bool use_separators = true;
423 struct config cfg = { 0 }; 430 struct config cfg = { 0 };
431 struct bpf_object *obj;
432 struct bpf_map *map;
424 char filename[256]; 433 char filename[256];
425 int longindex = 0; 434 int longindex = 0;
426 int interval = 2; 435 int interval = 2;
427 __u32 key = 0; 436 __u32 key = 0;
428 int opt, err;
429 437
430 char action_str_buf[XDP_ACTION_MAX_STRLEN + 1 /* for \0 */] = { 0 }; 438 char action_str_buf[XDP_ACTION_MAX_STRLEN + 1 /* for \0 */] = { 0 };
431 int action = XDP_PASS; /* Default action */ 439 int action = XDP_PASS; /* Default action */
432 char *action_str = NULL; 440 char *action_str = NULL;
433 441
434 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); 442 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
443 prog_load_attr.file = filename;
435 444
436 if (setrlimit(RLIMIT_MEMLOCK, &r)) { 445 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
437 perror("setrlimit(RLIMIT_MEMLOCK)"); 446 perror("setrlimit(RLIMIT_MEMLOCK)");
438 return 1; 447 return 1;
439 } 448 }
440 449
441 if (load_bpf_file(filename)) { 450 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
442 fprintf(stderr, "ERR in load_bpf_file(): %s", bpf_log_buf); 451 return EXIT_FAIL;
452
453 map = bpf_map__next(NULL, obj);
454 stats_global_map = bpf_map__next(map, obj);
455 rx_queue_index_map = bpf_map__next(stats_global_map, obj);
456 if (!map || !stats_global_map || !rx_queue_index_map) {
457 printf("finding a map in obj file failed\n");
443 return EXIT_FAIL; 458 return EXIT_FAIL;
444 } 459 }
460 map_fd = bpf_map__fd(map);
445 461
446 if (!prog_fd[0]) { 462 if (!prog_fd) {
447 fprintf(stderr, "ERR: load_bpf_file: %s\n", strerror(errno)); 463 fprintf(stderr, "ERR: load_bpf_file: %s\n", strerror(errno));
448 return EXIT_FAIL; 464 return EXIT_FAIL;
449 } 465 }
@@ -512,7 +528,7 @@ int main(int argc, char **argv)
512 setlocale(LC_NUMERIC, "en_US"); 528 setlocale(LC_NUMERIC, "en_US");
513 529
514 /* User-side setup ifindex in config_map */ 530 /* User-side setup ifindex in config_map */
515 err = bpf_map_update_elem(map_fd[0], &key, &cfg, 0); 531 err = bpf_map_update_elem(map_fd, &key, &cfg, 0);
516 if (err) { 532 if (err) {
517 fprintf(stderr, "Store config failed (err:%d)\n", err); 533 fprintf(stderr, "Store config failed (err:%d)\n", err);
518 exit(EXIT_FAIL_BPF); 534 exit(EXIT_FAIL_BPF);
@@ -521,7 +537,7 @@ int main(int argc, char **argv)
521 /* Remove XDP program when program is interrupted */ 537 /* Remove XDP program when program is interrupted */
522 signal(SIGINT, int_exit); 538 signal(SIGINT, int_exit);
523 539
524 if (bpf_set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) { 540 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
525 fprintf(stderr, "link set xdp fd failed\n"); 541 fprintf(stderr, "link set xdp fd failed\n");
526 return EXIT_FAIL_XDP; 542 return EXIT_FAIL_XDP;
527 } 543 }