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>
41 #include <linux/version.h>
52 static char **last_argv;
53 static int (*last_do_help)(int argc, char **argv);
54 json_writer_t *json_wtr;
58 struct pinned_obj_table prog_table;
59 struct pinned_obj_table map_table;
61 static void __noreturn clean_and_exit(int i)
64 jsonw_destroy(&json_wtr);
71 last_do_help(last_argc - 1, last_argv + 1);
76 static int do_help(int argc, char **argv)
84 "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
85 " %s batch file FILE\n"
88 " OBJECT := { prog | map | cgroup }\n"
89 " " HELP_SPEC_OPTIONS "\n"
91 bin_name, bin_name, bin_name);
96 static int do_version(int argc, char **argv)
98 unsigned int version[3];
100 version[0] = LINUX_VERSION_CODE >> 16;
101 version[1] = LINUX_VERSION_CODE >> 8 & 0xf;
102 version[2] = LINUX_VERSION_CODE & 0xf;
105 jsonw_start_object(json_wtr);
106 jsonw_name(json_wtr, "version");
107 jsonw_printf(json_wtr, "\"%u.%u.%u\"",
108 version[0], version[1], version[2]);
109 jsonw_end_object(json_wtr);
111 printf("%s v%u.%u.%u\n", bin_name,
112 version[0], version[1], version[2]);
117 int cmd_select(const struct cmd *cmds, int argc, char **argv,
118 int (*help)(int argc, char **argv))
126 if (argc < 1 && cmds[0].func)
127 return cmds[0].func(argc, argv);
129 for (i = 0; cmds[i].func; i++)
130 if (is_prefix(*argv, cmds[i].cmd))
131 return cmds[i].func(argc - 1, argv + 1);
133 help(argc - 1, argv + 1);
138 bool is_prefix(const char *pfx, const char *str)
142 if (strlen(str) < strlen(pfx))
145 return !memcmp(str, pfx, strlen(pfx));
148 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
150 unsigned char *data = arg;
153 for (i = 0; i < n; i++) {
154 const char *pfx = "";
165 fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
169 static int do_batch(int argc, char **argv);
171 static const struct cmd cmds[] = {
173 { "batch", do_batch },
176 { "cgroup", do_cgroup },
177 { "version", do_version },
181 static int do_batch(int argc, char **argv)
183 unsigned int lines = 0;
192 p_err("too few parameters for batch");
194 } else if (!is_prefix(*argv, "file")) {
195 p_err("expected 'file', got: %s", *argv);
197 } else if (argc > 2) {
198 p_err("too many parameters for batch");
203 fp = fopen(*argv, "r");
205 p_err("Can't open file (%s): %s", *argv, strerror(errno));
210 jsonw_start_array(json_wtr);
211 while (fgets(buf, sizeof(buf), fp)) {
212 if (strlen(buf) == sizeof(buf) - 1) {
218 n_argv[n_argc] = strtok(buf, " \t\n");
220 while (n_argv[n_argc]) {
222 if (n_argc == ARRAY_SIZE(n_argv)) {
223 p_err("line %d has too many arguments, skip",
228 n_argv[n_argc] = strtok(NULL, " \t\n");
235 jsonw_start_object(json_wtr);
236 jsonw_name(json_wtr, "command");
237 jsonw_start_array(json_wtr);
238 for (i = 0; i < n_argc; i++)
239 jsonw_string(json_wtr, n_argv[i]);
240 jsonw_end_array(json_wtr);
241 jsonw_name(json_wtr, "output");
244 err = cmd_select(cmds, n_argc, n_argv, do_help);
247 jsonw_end_object(json_wtr);
255 if (errno && errno != ENOENT) {
256 perror("reading batch file failed");
259 p_info("processed %d lines", lines);
266 jsonw_end_array(json_wtr);
271 int main(int argc, char **argv)
273 static const struct option options[] = {
274 { "json", no_argument, NULL, 'j' },
275 { "help", no_argument, NULL, 'h' },
276 { "pretty", no_argument, NULL, 'p' },
277 { "version", no_argument, NULL, 'V' },
278 { "bpffs", no_argument, NULL, 'f' },
283 last_do_help = do_help;
284 pretty_output = false;
289 hash_init(prog_table.table);
290 hash_init(map_table.table);
293 while ((opt = getopt_long(argc, argv, "Vhpjf",
294 options, NULL)) >= 0) {
297 return do_version(argc, argv);
299 return do_help(argc, argv);
301 pretty_output = true;
305 json_wtr = jsonw_new(stdout);
307 p_err("failed to create JSON writer");
312 jsonw_pretty(json_wtr, pretty_output);
318 p_err("unrecognized option '%s'", argv[optind - 1]);
333 ret = cmd_select(cmds, argc, argv, do_help);
336 jsonw_destroy(&json_wtr);
339 delete_pinned_obj_table(&prog_table);
340 delete_pinned_obj_table(&map_table);