]>
Commit | Line | Data |
---|---|---|
a3f74617 MKL |
1 | /* Copyright (c) 2016 Facebook |
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 | */ | |
7 | #include <linux/unistd.h> | |
8 | #include <linux/bpf.h> | |
9 | ||
10 | #include <stdio.h> | |
11 | #include <stdint.h> | |
12 | #include <unistd.h> | |
13 | #include <string.h> | |
14 | #include <errno.h> | |
15 | #include <fcntl.h> | |
16 | ||
17 | #include "libbpf.h" | |
18 | ||
19 | static void usage(void) | |
20 | { | |
21 | printf("Usage: test_cgrp2_array_pin [...]\n"); | |
22 | printf(" -F <file> File to pin an BPF cgroup array\n"); | |
23 | printf(" -U <file> Update an already pinned BPF cgroup array\n"); | |
24 | printf(" -v <value> Full path of the cgroup2\n"); | |
25 | printf(" -h Display this help\n"); | |
26 | } | |
27 | ||
28 | int main(int argc, char **argv) | |
29 | { | |
30 | const char *pinned_file = NULL, *cg2 = NULL; | |
31 | int create_array = 1; | |
32 | int array_key = 0; | |
33 | int array_fd = -1; | |
34 | int cg2_fd = -1; | |
35 | int ret = -1; | |
36 | int opt; | |
37 | ||
38 | while ((opt = getopt(argc, argv, "F:U:v:")) != -1) { | |
39 | switch (opt) { | |
40 | /* General args */ | |
41 | case 'F': | |
42 | pinned_file = optarg; | |
43 | break; | |
44 | case 'U': | |
45 | pinned_file = optarg; | |
46 | create_array = 0; | |
47 | break; | |
48 | case 'v': | |
49 | cg2 = optarg; | |
50 | break; | |
51 | default: | |
52 | usage(); | |
53 | goto out; | |
54 | } | |
55 | } | |
56 | ||
57 | if (!cg2 || !pinned_file) { | |
58 | usage(); | |
59 | goto out; | |
60 | } | |
61 | ||
62 | cg2_fd = open(cg2, O_RDONLY); | |
63 | if (cg2_fd < 0) { | |
64 | fprintf(stderr, "open(%s,...): %s(%d)\n", | |
65 | cg2, strerror(errno), errno); | |
66 | goto out; | |
67 | } | |
68 | ||
69 | if (create_array) { | |
70 | array_fd = bpf_create_map(BPF_MAP_TYPE_CGROUP_ARRAY, | |
71 | sizeof(uint32_t), sizeof(uint32_t), | |
72 | 1, 0); | |
73 | if (array_fd < 0) { | |
74 | fprintf(stderr, | |
75 | "bpf_create_map(BPF_MAP_TYPE_CGROUP_ARRAY,...): %s(%d)\n", | |
76 | strerror(errno), errno); | |
77 | goto out; | |
78 | } | |
79 | } else { | |
80 | array_fd = bpf_obj_get(pinned_file); | |
81 | if (array_fd < 0) { | |
82 | fprintf(stderr, "bpf_obj_get(%s): %s(%d)\n", | |
83 | pinned_file, strerror(errno), errno); | |
84 | goto out; | |
85 | } | |
86 | } | |
87 | ||
d40fc181 | 88 | ret = bpf_map_update_elem(array_fd, &array_key, &cg2_fd, 0); |
a3f74617 | 89 | if (ret) { |
d40fc181 | 90 | perror("bpf_map_update_elem"); |
a3f74617 MKL |
91 | goto out; |
92 | } | |
93 | ||
94 | if (create_array) { | |
95 | ret = bpf_obj_pin(array_fd, pinned_file); | |
96 | if (ret) { | |
97 | fprintf(stderr, "bpf_obj_pin(..., %s): %s(%d)\n", | |
98 | pinned_file, strerror(errno), errno); | |
99 | goto out; | |
100 | } | |
101 | } | |
102 | ||
103 | out: | |
104 | if (array_fd != -1) | |
105 | close(array_fd); | |
106 | if (cg2_fd != -1) | |
107 | close(cg2_fd); | |
108 | return ret; | |
109 | } |