2 * Copyright (C) 2017 Netronome Systems, Inc.
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.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 /* Author: Jakub Kicinski <kubakici@wp.pl> */
40 #include <linux/bpf.h>
51 static char **last_argv;
52 static int (*last_do_help)(int argc, char **argv);
53 json_writer_t *json_wtr;
57 struct pinned_obj_table prog_table;
58 struct pinned_obj_table map_table;
60 static void __noreturn clean_and_exit(int i)
63 jsonw_destroy(&json_wtr);
70 last_do_help(last_argc - 1, last_argv + 1);
75 static int do_help(int argc, char **argv)
83 "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
84 " %s batch file FILE\n"
87 " OBJECT := { prog | map | cgroup }\n"
88 " " HELP_SPEC_OPTIONS "\n"
90 bin_name, bin_name, bin_name);
95 static int do_version(int argc, char **argv)
98 jsonw_start_object(json_wtr);
99 jsonw_name(json_wtr, "version");
100 jsonw_printf(json_wtr, "\"%s\"", BPFTOOL_VERSION);
101 jsonw_end_object(json_wtr);
103 printf("%s v%s\n", bin_name, BPFTOOL_VERSION);
108 int cmd_select(const struct cmd *cmds, int argc, char **argv,
109 int (*help)(int argc, char **argv))
117 if (argc < 1 && cmds[0].func)
118 return cmds[0].func(argc, argv);
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);
124 help(argc - 1, argv + 1);
129 bool is_prefix(const char *pfx, const char *str)
133 if (strlen(str) < strlen(pfx))
136 return !memcmp(str, pfx, strlen(pfx));
139 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
141 unsigned char *data = arg;
144 for (i = 0; i < n; i++) {
145 const char *pfx = "";
156 fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
160 static int do_batch(int argc, char **argv);
162 static const struct cmd cmds[] = {
164 { "batch", do_batch },
167 { "cgroup", do_cgroup },
168 { "version", do_version },
172 static int do_batch(int argc, char **argv)
174 unsigned int lines = 0;
183 p_err("too few parameters for batch");
185 } else if (!is_prefix(*argv, "file")) {
186 p_err("expected 'file', got: %s", *argv);
188 } else if (argc > 2) {
189 p_err("too many parameters for batch");
194 fp = fopen(*argv, "r");
196 p_err("Can't open file (%s): %s", *argv, strerror(errno));
201 jsonw_start_array(json_wtr);
202 while (fgets(buf, sizeof(buf), fp)) {
203 if (strlen(buf) == sizeof(buf) - 1) {
209 n_argv[n_argc] = strtok(buf, " \t\n");
211 while (n_argv[n_argc]) {
213 if (n_argc == ARRAY_SIZE(n_argv)) {
214 p_err("line %d has too many arguments, skip",
219 n_argv[n_argc] = strtok(NULL, " \t\n");
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");
235 err = cmd_select(cmds, n_argc, n_argv, do_help);
238 jsonw_end_object(json_wtr);
246 if (errno && errno != ENOENT) {
247 perror("reading batch file failed");
250 p_info("processed %d lines", lines);
257 jsonw_end_array(json_wtr);
262 int main(int argc, char **argv)
264 static const struct option options[] = {
265 { "json", no_argument, NULL, 'j' },
266 { "help", no_argument, NULL, 'h' },
267 { "pretty", no_argument, NULL, 'p' },
268 { "version", no_argument, NULL, 'V' },
269 { "bpffs", no_argument, NULL, 'f' },
274 last_do_help = do_help;
275 pretty_output = false;
280 hash_init(prog_table.table);
281 hash_init(map_table.table);
284 while ((opt = getopt_long(argc, argv, "Vhpjf",
285 options, NULL)) >= 0) {
288 return do_version(argc, argv);
290 return do_help(argc, argv);
292 pretty_output = true;
296 json_wtr = jsonw_new(stdout);
298 p_err("failed to create JSON writer");
303 jsonw_pretty(json_wtr, pretty_output);
309 p_err("unrecognized option '%s'", argv[optind - 1]);
324 ret = cmd_select(cmds, argc, argv, do_help);
327 jsonw_destroy(&json_wtr);
330 delete_pinned_obj_table(&prog_table);
331 delete_pinned_obj_table(&map_table);