| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* Copyright (c) 2015 PLUMgrid, http://plumgrid.com |
|---|
| 2 | | - * |
|---|
| 3 | | - * This program is free software; you can redistribute it and/or |
|---|
| 4 | | - * modify it under the terms of version 2 of the GNU General Public |
|---|
| 5 | | - * License as published by the Free Software Foundation. |
|---|
| 6 | 3 | */ |
|---|
| 7 | 4 | #include <stdio.h> |
|---|
| 8 | 5 | #include <stdlib.h> |
|---|
| .. | .. |
|---|
| 11 | 8 | #include <stdbool.h> |
|---|
| 12 | 9 | #include <string.h> |
|---|
| 13 | 10 | #include <time.h> |
|---|
| 14 | | -#include <linux/bpf.h> |
|---|
| 15 | 11 | #include <sys/resource.h> |
|---|
| 16 | 12 | |
|---|
| 17 | 13 | #include <bpf/bpf.h> |
|---|
| 18 | | -#include "bpf_load.h" |
|---|
| 14 | +#include <bpf/libbpf.h> |
|---|
| 19 | 15 | |
|---|
| 20 | 16 | struct pair { |
|---|
| 21 | 17 | long long val; |
|---|
| .. | .. |
|---|
| 39 | 35 | key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */ |
|---|
| 40 | 36 | |
|---|
| 41 | 37 | key = -1; |
|---|
| 42 | | - while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) { |
|---|
| 43 | | - bpf_map_lookup_elem(map_fd[0], &next_key, &v); |
|---|
| 38 | + while (bpf_map_get_next_key(fd, &key, &next_key) == 0) { |
|---|
| 39 | + bpf_map_lookup_elem(fd, &next_key, &v); |
|---|
| 44 | 40 | key = next_key; |
|---|
| 45 | 41 | if (val - v.val < 1000000000ll) |
|---|
| 46 | 42 | /* object was allocated more then 1 sec ago */ |
|---|
| .. | .. |
|---|
| 53 | 49 | int main(int ac, char **argv) |
|---|
| 54 | 50 | { |
|---|
| 55 | 51 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
|---|
| 52 | + struct bpf_link *links[2]; |
|---|
| 53 | + struct bpf_program *prog; |
|---|
| 54 | + struct bpf_object *obj; |
|---|
| 56 | 55 | char filename[256]; |
|---|
| 57 | | - int i; |
|---|
| 58 | | - |
|---|
| 59 | | - snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
|---|
| 56 | + int map_fd, i, j = 0; |
|---|
| 60 | 57 | |
|---|
| 61 | 58 | if (setrlimit(RLIMIT_MEMLOCK, &r)) { |
|---|
| 62 | 59 | perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)"); |
|---|
| 63 | 60 | return 1; |
|---|
| 64 | 61 | } |
|---|
| 65 | 62 | |
|---|
| 66 | | - if (load_bpf_file(filename)) { |
|---|
| 67 | | - printf("%s", bpf_log_buf); |
|---|
| 68 | | - return 1; |
|---|
| 63 | + snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
|---|
| 64 | + obj = bpf_object__open_file(filename, NULL); |
|---|
| 65 | + if (libbpf_get_error(obj)) { |
|---|
| 66 | + fprintf(stderr, "ERROR: opening BPF object file failed\n"); |
|---|
| 67 | + return 0; |
|---|
| 68 | + } |
|---|
| 69 | + |
|---|
| 70 | + /* load BPF program */ |
|---|
| 71 | + if (bpf_object__load(obj)) { |
|---|
| 72 | + fprintf(stderr, "ERROR: loading BPF object file failed\n"); |
|---|
| 73 | + goto cleanup; |
|---|
| 74 | + } |
|---|
| 75 | + |
|---|
| 76 | + map_fd = bpf_object__find_map_fd_by_name(obj, "my_map"); |
|---|
| 77 | + if (map_fd < 0) { |
|---|
| 78 | + fprintf(stderr, "ERROR: finding a map in obj file failed\n"); |
|---|
| 79 | + goto cleanup; |
|---|
| 80 | + } |
|---|
| 81 | + |
|---|
| 82 | + bpf_object__for_each_program(prog, obj) { |
|---|
| 83 | + links[j] = bpf_program__attach(prog); |
|---|
| 84 | + if (libbpf_get_error(links[j])) { |
|---|
| 85 | + fprintf(stderr, "ERROR: bpf_program__attach failed\n"); |
|---|
| 86 | + links[j] = NULL; |
|---|
| 87 | + goto cleanup; |
|---|
| 88 | + } |
|---|
| 89 | + j++; |
|---|
| 69 | 90 | } |
|---|
| 70 | 91 | |
|---|
| 71 | 92 | for (i = 0; ; i++) { |
|---|
| 72 | | - print_old_objects(map_fd[1]); |
|---|
| 93 | + print_old_objects(map_fd); |
|---|
| 73 | 94 | sleep(1); |
|---|
| 74 | 95 | } |
|---|
| 75 | 96 | |
|---|
| 97 | +cleanup: |
|---|
| 98 | + for (j--; j >= 0; j--) |
|---|
| 99 | + bpf_link__destroy(links[j]); |
|---|
| 100 | + |
|---|
| 101 | + bpf_object__close(obj); |
|---|
| 76 | 102 | return 0; |
|---|
| 77 | 103 | } |
|---|