blob: 6ad53f1797fade24e41223e2bd908be32fe2f839 [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>
Quentin Monnet821cfbb2017-10-19 15:46:26 -070041#include <linux/version.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070042#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45
46#include <bpf.h>
47
48#include "main.h"
49
50const char *bin_name;
51static int last_argc;
52static char **last_argv;
53static int (*last_do_help)(int argc, char **argv);
Quentin Monnetd35efba2017-10-23 09:24:07 -070054json_writer_t *json_wtr;
55bool pretty_output;
56bool json_output;
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
60void usage(void)
61{
62 last_do_help(last_argc - 1, last_argv + 1);
63
64 exit(-1);
65}
66
67static int do_help(int argc, char **argv)
68{
Quentin Monnet004b45c2017-10-23 09:24:14 -070069 if (json_output) {
70 jsonw_null(json_wtr);
71 return 0;
72 }
73
Jakub Kicinski71bb4282017-10-04 20:10:04 -070074 fprintf(stderr,
Quentin Monnet0641c3c2017-10-23 09:24:16 -070075 "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -070076 " %s batch file FILE\n"
Quentin Monnet821cfbb2017-10-19 15:46:26 -070077 " %s version\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -070078 "\n"
Quentin Monnet0641c3c2017-10-23 09:24:16 -070079 " OBJECT := { prog | map }\n"
80 " " HELP_SPEC_OPTIONS "\n"
81 "",
Quentin Monnet821cfbb2017-10-19 15:46:26 -070082 bin_name, bin_name, bin_name);
Jakub Kicinski71bb4282017-10-04 20:10:04 -070083
84 return 0;
85}
86
Quentin Monnet821cfbb2017-10-19 15:46:26 -070087static int do_version(int argc, char **argv)
88{
Quentin Monnet004b45c2017-10-23 09:24:14 -070089 unsigned int version[3];
90
91 version[0] = LINUX_VERSION_CODE >> 16;
92 version[1] = LINUX_VERSION_CODE >> 8 & 0xf;
93 version[2] = LINUX_VERSION_CODE & 0xf;
94
95 if (json_output) {
96 jsonw_start_object(json_wtr);
97 jsonw_name(json_wtr, "version");
98 jsonw_printf(json_wtr, "\"%u.%u.%u\"",
99 version[0], version[1], version[2]);
100 jsonw_end_object(json_wtr);
101 } else {
102 printf("%s v%u.%u.%u\n", bin_name,
103 version[0], version[1], version[2]);
104 }
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 },
Quentin Monnet821cfbb2017-10-19 15:46:26 -0700167 { "version", do_version },
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700168 { 0 }
169};
170
171static int do_batch(int argc, char **argv)
172{
173 unsigned int lines = 0;
174 char *n_argv[4096];
175 char buf[65536];
176 int n_argc;
177 FILE *fp;
178 int err;
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700179 int i;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700180
181 if (argc < 2) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700182 p_err("too few parameters for batch");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700183 return -1;
184 } else if (!is_prefix(*argv, "file")) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700185 p_err("expected 'file', got: %s", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700186 return -1;
187 } else if (argc > 2) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700188 p_err("too many parameters for batch");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700189 return -1;
190 }
191 NEXT_ARG();
192
193 fp = fopen(*argv, "r");
194 if (!fp) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700195 p_err("Can't open file (%s): %s", *argv, strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700196 return -1;
197 }
198
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700199 if (json_output)
200 jsonw_start_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700201 while (fgets(buf, sizeof(buf), fp)) {
202 if (strlen(buf) == sizeof(buf) - 1) {
203 errno = E2BIG;
204 break;
205 }
206
207 n_argc = 0;
208 n_argv[n_argc] = strtok(buf, " \t\n");
209
210 while (n_argv[n_argc]) {
211 n_argc++;
212 if (n_argc == ARRAY_SIZE(n_argv)) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700213 p_err("line %d has too many arguments, skip",
214 lines);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700215 n_argc = 0;
216 break;
217 }
218 n_argv[n_argc] = strtok(NULL, " \t\n");
219 }
220
221 if (!n_argc)
222 continue;
223
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700224 if (json_output) {
225 jsonw_start_object(json_wtr);
226 jsonw_name(json_wtr, "command");
227 jsonw_start_array(json_wtr);
228 for (i = 0; i < n_argc; i++)
229 jsonw_string(json_wtr, n_argv[i]);
230 jsonw_end_array(json_wtr);
231 jsonw_name(json_wtr, "output");
232 }
233
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700234 err = cmd_select(cmds, n_argc, n_argv, do_help);
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700235
236 if (json_output)
237 jsonw_end_object(json_wtr);
238
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700239 if (err)
240 goto err_close;
241
242 lines++;
243 }
244
245 if (errno && errno != ENOENT) {
246 perror("reading batch file failed");
247 err = -1;
248 } else {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700249 p_info("processed %d lines", lines);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700250 err = 0;
251 }
252err_close:
253 fclose(fp);
254
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700255 if (json_output)
256 jsonw_end_array(json_wtr);
257
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700258 return err;
259}
260
261int main(int argc, char **argv)
262{
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700263 static const struct option options[] = {
Quentin Monnetd35efba2017-10-23 09:24:07 -0700264 { "json", no_argument, NULL, 'j' },
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700265 { "help", no_argument, NULL, 'h' },
Quentin Monnetd35efba2017-10-23 09:24:07 -0700266 { "pretty", no_argument, NULL, 'p' },
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700267 { "version", no_argument, NULL, 'V' },
268 { 0 }
269 };
Quentin Monnetd35efba2017-10-23 09:24:07 -0700270 int opt, ret;
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700271
272 last_do_help = do_help;
Quentin Monnetd35efba2017-10-23 09:24:07 -0700273 pretty_output = false;
274 json_output = false;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700275 bin_name = argv[0];
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700276
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900277 hash_init(prog_table.table);
278 hash_init(map_table.table);
279
Quentin Monnetd35efba2017-10-23 09:24:07 -0700280 while ((opt = getopt_long(argc, argv, "Vhpj",
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700281 options, NULL)) >= 0) {
282 switch (opt) {
283 case 'V':
284 return do_version(argc, argv);
285 case 'h':
286 return do_help(argc, argv);
Quentin Monnetd35efba2017-10-23 09:24:07 -0700287 case 'p':
288 pretty_output = true;
289 /* fall through */
290 case 'j':
291 json_output = true;
292 break;
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700293 default:
294 usage();
295 }
296 }
297
298 argc -= optind;
299 argv += optind;
300 if (argc < 0)
301 usage();
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700302
Quentin Monnetd35efba2017-10-23 09:24:07 -0700303 if (json_output) {
304 json_wtr = jsonw_new(stdout);
305 if (!json_wtr) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700306 p_err("failed to create JSON writer");
Quentin Monnetd35efba2017-10-23 09:24:07 -0700307 return -1;
308 }
309 jsonw_pretty(json_wtr, pretty_output);
310 }
311
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700312 bfd_init();
313
Quentin Monnetd35efba2017-10-23 09:24:07 -0700314 ret = cmd_select(cmds, argc, argv, do_help);
315
316 if (json_output)
317 jsonw_destroy(&json_wtr);
318
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900319 delete_pinned_obj_table(&prog_table);
320 delete_pinned_obj_table(&map_table);
321
Quentin Monnetd35efba2017-10-23 09:24:07 -0700322 return ret;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700323}