.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com |
---|
2 | 3 | * Copyright (c) 2015 BMW Car IT GmbH |
---|
3 | | - * |
---|
4 | | - * This program is free software; you can redistribute it and/or |
---|
5 | | - * modify it under the terms of version 2 of the GNU General Public |
---|
6 | | - * License as published by the Free Software Foundation. |
---|
7 | 4 | */ |
---|
8 | 5 | #include <stdio.h> |
---|
9 | 6 | #include <unistd.h> |
---|
10 | 7 | #include <stdlib.h> |
---|
11 | 8 | #include <signal.h> |
---|
12 | | -#include <linux/bpf.h> |
---|
| 9 | +#include <bpf/libbpf.h> |
---|
13 | 10 | #include <bpf/bpf.h> |
---|
14 | | -#include "bpf_load.h" |
---|
15 | 11 | |
---|
16 | 12 | #define MAX_ENTRIES 20 |
---|
17 | 13 | #define MAX_CPU 4 |
---|
.. | .. |
---|
84 | 80 | |
---|
85 | 81 | int main(int argc, char **argv) |
---|
86 | 82 | { |
---|
| 83 | + struct bpf_link *links[2]; |
---|
| 84 | + struct bpf_program *prog; |
---|
| 85 | + struct bpf_object *obj; |
---|
87 | 86 | char filename[256]; |
---|
| 87 | + int map_fd, i = 0; |
---|
88 | 88 | |
---|
89 | 89 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
---|
| 90 | + obj = bpf_object__open_file(filename, NULL); |
---|
| 91 | + if (libbpf_get_error(obj)) { |
---|
| 92 | + fprintf(stderr, "ERROR: opening BPF object file failed\n"); |
---|
| 93 | + return 0; |
---|
| 94 | + } |
---|
90 | 95 | |
---|
91 | | - if (load_bpf_file(filename)) { |
---|
92 | | - printf("%s", bpf_log_buf); |
---|
93 | | - return 1; |
---|
| 96 | + /* load BPF program */ |
---|
| 97 | + if (bpf_object__load(obj)) { |
---|
| 98 | + fprintf(stderr, "ERROR: loading BPF object file failed\n"); |
---|
| 99 | + goto cleanup; |
---|
| 100 | + } |
---|
| 101 | + |
---|
| 102 | + map_fd = bpf_object__find_map_fd_by_name(obj, "my_lat"); |
---|
| 103 | + if (map_fd < 0) { |
---|
| 104 | + fprintf(stderr, "ERROR: finding a map in obj file failed\n"); |
---|
| 105 | + goto cleanup; |
---|
| 106 | + } |
---|
| 107 | + |
---|
| 108 | + bpf_object__for_each_program(prog, obj) { |
---|
| 109 | + links[i] = bpf_program__attach(prog); |
---|
| 110 | + if (libbpf_get_error(links[i])) { |
---|
| 111 | + fprintf(stderr, "ERROR: bpf_program__attach failed\n"); |
---|
| 112 | + links[i] = NULL; |
---|
| 113 | + goto cleanup; |
---|
| 114 | + } |
---|
| 115 | + i++; |
---|
94 | 116 | } |
---|
95 | 117 | |
---|
96 | 118 | while (1) { |
---|
97 | | - get_data(map_fd[1]); |
---|
| 119 | + get_data(map_fd); |
---|
98 | 120 | print_hist(); |
---|
99 | 121 | sleep(5); |
---|
100 | 122 | } |
---|
101 | 123 | |
---|
| 124 | +cleanup: |
---|
| 125 | + for (i--; i >= 0; i--) |
---|
| 126 | + bpf_link__destroy(links[i]); |
---|
| 127 | + |
---|
| 128 | + bpf_object__close(obj); |
---|
102 | 129 | return 0; |
---|
103 | 130 | } |
---|