^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include "trace_helpers.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) int main(int ac, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) struct bpf_link *link = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) struct bpf_program *prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) struct bpf_object *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) char filename[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) FILE *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) obj = bpf_object__open_file(filename, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) if (libbpf_get_error(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) fprintf(stderr, "ERROR: opening BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (!prog) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) fprintf(stderr, "ERROR: finding a prog in obj file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /* load BPF program */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (bpf_object__load(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) fprintf(stderr, "ERROR: loading BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) link = bpf_program__attach(prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (libbpf_get_error(link)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) fprintf(stderr, "ERROR: bpf_program__attach failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) link = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) f = popen("taskset 1 ping -c5 localhost", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) (void) f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) read_trace_pipe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) bpf_link__destroy(link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) bpf_object__close(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }