.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * Copyright (c) 2017 Facebook |
---|
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 <sys/resource.h> |
---|
9 | 6 | #include <sys/socket.h> |
---|
.. | .. |
---|
14 | 11 | #include <stdlib.h> |
---|
15 | 12 | #include <stdio.h> |
---|
16 | 13 | #include <bpf/bpf.h> |
---|
17 | | -#include "bpf_load.h" |
---|
| 14 | +#include <bpf/libbpf.h> |
---|
| 15 | + |
---|
| 16 | +static int map_fd[7]; |
---|
18 | 17 | |
---|
19 | 18 | #define PORT_A (map_fd[0]) |
---|
20 | 19 | #define PORT_H (map_fd[1]) |
---|
.. | .. |
---|
116 | 115 | int main(int argc, char **argv) |
---|
117 | 116 | { |
---|
118 | 117 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
---|
| 118 | + struct bpf_link *link = NULL; |
---|
| 119 | + struct bpf_program *prog; |
---|
| 120 | + struct bpf_object *obj; |
---|
119 | 121 | char filename[256]; |
---|
120 | 122 | |
---|
121 | | - assert(!setrlimit(RLIMIT_MEMLOCK, &r)); |
---|
| 123 | + if (setrlimit(RLIMIT_MEMLOCK, &r)) { |
---|
| 124 | + perror("setrlimit(RLIMIT_MEMLOCK)"); |
---|
| 125 | + return 1; |
---|
| 126 | + } |
---|
122 | 127 | |
---|
123 | 128 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
---|
| 129 | + obj = bpf_object__open_file(filename, NULL); |
---|
| 130 | + if (libbpf_get_error(obj)) { |
---|
| 131 | + fprintf(stderr, "ERROR: opening BPF object file failed\n"); |
---|
| 132 | + return 0; |
---|
| 133 | + } |
---|
124 | 134 | |
---|
125 | | - if (load_bpf_file(filename)) { |
---|
126 | | - printf("%s", bpf_log_buf); |
---|
127 | | - return 1; |
---|
| 135 | + prog = bpf_object__find_program_by_name(obj, "trace_sys_connect"); |
---|
| 136 | + if (!prog) { |
---|
| 137 | + printf("finding a prog in obj file failed\n"); |
---|
| 138 | + goto cleanup; |
---|
| 139 | + } |
---|
| 140 | + |
---|
| 141 | + /* load BPF program */ |
---|
| 142 | + if (bpf_object__load(obj)) { |
---|
| 143 | + fprintf(stderr, "ERROR: loading BPF object file failed\n"); |
---|
| 144 | + goto cleanup; |
---|
| 145 | + } |
---|
| 146 | + |
---|
| 147 | + map_fd[0] = bpf_object__find_map_fd_by_name(obj, "port_a"); |
---|
| 148 | + map_fd[1] = bpf_object__find_map_fd_by_name(obj, "port_h"); |
---|
| 149 | + map_fd[2] = bpf_object__find_map_fd_by_name(obj, "reg_result_h"); |
---|
| 150 | + map_fd[3] = bpf_object__find_map_fd_by_name(obj, "inline_result_h"); |
---|
| 151 | + map_fd[4] = bpf_object__find_map_fd_by_name(obj, "a_of_port_a"); |
---|
| 152 | + map_fd[5] = bpf_object__find_map_fd_by_name(obj, "h_of_port_a"); |
---|
| 153 | + map_fd[6] = bpf_object__find_map_fd_by_name(obj, "h_of_port_h"); |
---|
| 154 | + if (map_fd[0] < 0 || map_fd[1] < 0 || map_fd[2] < 0 || |
---|
| 155 | + map_fd[3] < 0 || map_fd[4] < 0 || map_fd[5] < 0 || map_fd[6] < 0) { |
---|
| 156 | + fprintf(stderr, "ERROR: finding a map in obj file failed\n"); |
---|
| 157 | + goto cleanup; |
---|
| 158 | + } |
---|
| 159 | + |
---|
| 160 | + link = bpf_program__attach(prog); |
---|
| 161 | + if (libbpf_get_error(link)) { |
---|
| 162 | + fprintf(stderr, "ERROR: bpf_program__attach failed\n"); |
---|
| 163 | + link = NULL; |
---|
| 164 | + goto cleanup; |
---|
128 | 165 | } |
---|
129 | 166 | |
---|
130 | 167 | test_map_in_map(); |
---|
131 | 168 | |
---|
| 169 | +cleanup: |
---|
| 170 | + bpf_link__destroy(link); |
---|
| 171 | + bpf_object__close(obj); |
---|
132 | 172 | return 0; |
---|
133 | 173 | } |
---|