blob: 185acfa229b592f0bf0b62d972e90c3746959283 [file] [log] [blame]
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001/*
2 * Copyright (C) 2017 Netronome Systems, Inc.
3 *
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
8 *
9 * The BSD 2-Clause License:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34/* Author: Jakub Kicinski <kubakici@wp.pl> */
35
36#include <bfd.h>
37#include <ctype.h>
38#include <errno.h>
Quentin Monneta2bc2e52017-10-23 09:24:06 -070039#include <getopt.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070040#include <linux/bpf.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44
45#include <bpf.h>
46
47#include "main.h"
48
49const char *bin_name;
50static int last_argc;
51static char **last_argv;
52static int (*last_do_help)(int argc, char **argv);
Quentin Monnetd35efba2017-10-23 09:24:07 -070053json_writer_t *json_wtr;
54bool pretty_output;
55bool json_output;
Prashant Bholec541b732017-11-08 13:55:49 +090056bool show_pinned;
Prashant Bhole4990f1f2017-11-08 13:55:48 +090057struct pinned_obj_table prog_table;
58struct pinned_obj_table map_table;
Jakub Kicinski71bb4282017-10-04 20:10:04 -070059
Quentin Monnet78686202017-11-28 17:44:29 -080060static void __noreturn clean_and_exit(int i)
61{
62 if (json_output)
63 jsonw_destroy(&json_wtr);
64
65 exit(i);
66}
67
Jakub Kicinski71bb4282017-10-04 20:10:04 -070068void usage(void)
69{
70 last_do_help(last_argc - 1, last_argv + 1);
71
Quentin Monnet78686202017-11-28 17:44:29 -080072 clean_and_exit(-1);
Jakub Kicinski71bb4282017-10-04 20:10:04 -070073}
74
75static int do_help(int argc, char **argv)
76{
Quentin Monnet004b45c2017-10-23 09:24:14 -070077 if (json_output) {
78 jsonw_null(json_wtr);
79 return 0;
80 }
81
Jakub Kicinski71bb4282017-10-04 20:10:04 -070082 fprintf(stderr,
Quentin Monnet0641c3c2017-10-23 09:24:16 -070083 "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -070084 " %s batch file FILE\n"
Quentin Monnet821cfbb2017-10-19 15:46:26 -070085 " %s version\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -070086 "\n"
Roman Gushchin5ccda642017-12-13 15:18:54 +000087 " OBJECT := { prog | map | cgroup }\n"
Quentin Monnet0641c3c2017-10-23 09:24:16 -070088 " " HELP_SPEC_OPTIONS "\n"
89 "",
Quentin Monnet821cfbb2017-10-19 15:46:26 -070090 bin_name, bin_name, bin_name);
Jakub Kicinski71bb4282017-10-04 20:10:04 -070091
92 return 0;
93}
94
Quentin Monnet821cfbb2017-10-19 15:46:26 -070095static int do_version(int argc, char **argv)
96{
Quentin Monnet004b45c2017-10-23 09:24:14 -070097 if (json_output) {
98 jsonw_start_object(json_wtr);
99 jsonw_name(json_wtr, "version");
Roman Gushchin4bfe3bd2017-12-27 19:16:28 +0000100 jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
Quentin Monnet004b45c2017-10-23 09:24:14 -0700101 jsonw_end_object(json_wtr);
102 } else {
Roman Gushchin4bfe3bd2017-12-27 19:16:28 +0000103 printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
Quentin Monnet004b45c2017-10-23 09:24:14 -0700104 }
Quentin Monnet821cfbb2017-10-19 15:46:26 -0700105 return 0;
106}
107
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700108int cmd_select(const struct cmd *cmds, int argc, char **argv,
109 int (*help)(int argc, char **argv))
110{
111 unsigned int i;
112
113 last_argc = argc;
114 last_argv = argv;
115 last_do_help = help;
116
117 if (argc < 1 && cmds[0].func)
118 return cmds[0].func(argc, argv);
119
120 for (i = 0; cmds[i].func; i++)
121 if (is_prefix(*argv, cmds[i].cmd))
122 return cmds[i].func(argc - 1, argv + 1);
123
124 help(argc - 1, argv + 1);
125
126 return -1;
127}
128
129bool is_prefix(const char *pfx, const char *str)
130{
131 if (!pfx)
132 return false;
133 if (strlen(str) < strlen(pfx))
134 return false;
135
136 return !memcmp(str, pfx, strlen(pfx));
137}
138
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700139void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700140{
141 unsigned char *data = arg;
142 unsigned int i;
143
144 for (i = 0; i < n; i++) {
145 const char *pfx = "";
146
147 if (!i)
148 /* nothing */;
149 else if (!(i % 16))
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700150 fprintf(f, "\n");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700151 else if (!(i % 8))
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700152 fprintf(f, " ");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700153 else
154 pfx = sep;
155
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700156 fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700157 }
158}
159
160static int do_batch(int argc, char **argv);
161
162static const struct cmd cmds[] = {
163 { "help", do_help },
164 { "batch", do_batch },
165 { "prog", do_prog },
166 { "map", do_map },
Roman Gushchin5ccda642017-12-13 15:18:54 +0000167 { "cgroup", do_cgroup },
Quentin Monnet821cfbb2017-10-19 15:46:26 -0700168 { "version", do_version },
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700169 { 0 }
170};
171
172static int do_batch(int argc, char **argv)
173{
174 unsigned int lines = 0;
175 char *n_argv[4096];
176 char buf[65536];
177 int n_argc;
178 FILE *fp;
179 int err;
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700180 int i;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700181
182 if (argc < 2) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700183 p_err("too few parameters for batch");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700184 return -1;
185 } else if (!is_prefix(*argv, "file")) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700186 p_err("expected 'file', got: %s", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700187 return -1;
188 } else if (argc > 2) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700189 p_err("too many parameters for batch");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700190 return -1;
191 }
192 NEXT_ARG();
193
194 fp = fopen(*argv, "r");
195 if (!fp) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700196 p_err("Can't open file (%s): %s", *argv, strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700197 return -1;
198 }
199
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700200 if (json_output)
201 jsonw_start_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700202 while (fgets(buf, sizeof(buf), fp)) {
203 if (strlen(buf) == sizeof(buf) - 1) {
204 errno = E2BIG;
205 break;
206 }
207
208 n_argc = 0;
209 n_argv[n_argc] = strtok(buf, " \t\n");
210
211 while (n_argv[n_argc]) {
212 n_argc++;
213 if (n_argc == ARRAY_SIZE(n_argv)) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700214 p_err("line %d has too many arguments, skip",
215 lines);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700216 n_argc = 0;
217 break;
218 }
219 n_argv[n_argc] = strtok(NULL, " \t\n");
220 }
221
222 if (!n_argc)
223 continue;
224
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700225 if (json_output) {
226 jsonw_start_object(json_wtr);
227 jsonw_name(json_wtr, "command");
228 jsonw_start_array(json_wtr);
229 for (i = 0; i < n_argc; i++)
230 jsonw_string(json_wtr, n_argv[i]);
231 jsonw_end_array(json_wtr);
232 jsonw_name(json_wtr, "output");
233 }
234
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700235 err = cmd_select(cmds, n_argc, n_argv, do_help);
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700236
237 if (json_output)
238 jsonw_end_object(json_wtr);
239
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700240 if (err)
241 goto err_close;
242
243 lines++;
244 }
245
246 if (errno && errno != ENOENT) {
Quentin Monnet9be6d412018-02-14 22:42:55 -0800247 p_err("reading batch file failed: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700248 err = -1;
249 } else {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700250 p_info("processed %d lines", lines);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700251 err = 0;
252 }
253err_close:
254 fclose(fp);
255
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700256 if (json_output)
257 jsonw_end_array(json_wtr);
258
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700259 return err;
260}
261
262int main(int argc, char **argv)
263{
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700264 static const struct option options[] = {
Quentin Monnetd35efba2017-10-23 09:24:07 -0700265 { "json", no_argument, NULL, 'j' },
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700266 { "help", no_argument, NULL, 'h' },
Quentin Monnetd35efba2017-10-23 09:24:07 -0700267 { "pretty", no_argument, NULL, 'p' },
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700268 { "version", no_argument, NULL, 'V' },
Prashant Bholec541b732017-11-08 13:55:49 +0900269 { "bpffs", no_argument, NULL, 'f' },
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700270 { 0 }
271 };
Quentin Monnetd35efba2017-10-23 09:24:07 -0700272 int opt, ret;
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700273
274 last_do_help = do_help;
Quentin Monnetd35efba2017-10-23 09:24:07 -0700275 pretty_output = false;
276 json_output = false;
Prashant Bholec541b732017-11-08 13:55:49 +0900277 show_pinned = false;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700278 bin_name = argv[0];
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700279
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900280 hash_init(prog_table.table);
281 hash_init(map_table.table);
282
Quentin Monnet146882a2017-11-28 17:44:30 -0800283 opterr = 0;
Prashant Bholec541b732017-11-08 13:55:49 +0900284 while ((opt = getopt_long(argc, argv, "Vhpjf",
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700285 options, NULL)) >= 0) {
286 switch (opt) {
287 case 'V':
288 return do_version(argc, argv);
289 case 'h':
290 return do_help(argc, argv);
Quentin Monnetd35efba2017-10-23 09:24:07 -0700291 case 'p':
292 pretty_output = true;
293 /* fall through */
294 case 'j':
Quentin Monnet9b85c2d2017-11-28 17:44:28 -0800295 if (!json_output) {
296 json_wtr = jsonw_new(stdout);
297 if (!json_wtr) {
298 p_err("failed to create JSON writer");
299 return -1;
300 }
301 json_output = true;
302 }
303 jsonw_pretty(json_wtr, pretty_output);
Quentin Monnetd35efba2017-10-23 09:24:07 -0700304 break;
Prashant Bholec541b732017-11-08 13:55:49 +0900305 case 'f':
306 show_pinned = true;
307 break;
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700308 default:
Quentin Monnet146882a2017-11-28 17:44:30 -0800309 p_err("unrecognized option '%s'", argv[optind - 1]);
310 if (json_output)
311 clean_and_exit(-1);
312 else
313 usage();
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700314 }
315 }
316
317 argc -= optind;
318 argv += optind;
319 if (argc < 0)
320 usage();
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700321
322 bfd_init();
323
Quentin Monnetd35efba2017-10-23 09:24:07 -0700324 ret = cmd_select(cmds, argc, argv, do_help);
325
326 if (json_output)
327 jsonw_destroy(&json_wtr);
328
Prashant Bholec541b732017-11-08 13:55:49 +0900329 if (show_pinned) {
330 delete_pinned_obj_table(&prog_table);
331 delete_pinned_obj_table(&map_table);
332 }
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900333
Quentin Monnetd35efba2017-10-23 09:24:07 -0700334 return ret;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700335}