blob: 78d9afb74ef4f82514e0ce47bc3f761f1032584d [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;
Jakub Kicinski71bb4282017-10-04 20:10:04 -070057
58void usage(void)
59{
60 last_do_help(last_argc - 1, last_argv + 1);
61
62 exit(-1);
63}
64
65static int do_help(int argc, char **argv)
66{
Quentin Monnet004b45c2017-10-23 09:24:14 -070067 if (json_output) {
68 jsonw_null(json_wtr);
69 return 0;
70 }
71
Jakub Kicinski71bb4282017-10-04 20:10:04 -070072 fprintf(stderr,
Quentin Monnet0641c3c2017-10-23 09:24:16 -070073 "Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -070074 " %s batch file FILE\n"
Quentin Monnet821cfbb2017-10-19 15:46:26 -070075 " %s version\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -070076 "\n"
Quentin Monnet0641c3c2017-10-23 09:24:16 -070077 " OBJECT := { prog | map }\n"
78 " " HELP_SPEC_OPTIONS "\n"
79 "",
Quentin Monnet821cfbb2017-10-19 15:46:26 -070080 bin_name, bin_name, bin_name);
Jakub Kicinski71bb4282017-10-04 20:10:04 -070081
82 return 0;
83}
84
Quentin Monnet821cfbb2017-10-19 15:46:26 -070085static int do_version(int argc, char **argv)
86{
Quentin Monnet004b45c2017-10-23 09:24:14 -070087 unsigned int version[3];
88
89 version[0] = LINUX_VERSION_CODE >> 16;
90 version[1] = LINUX_VERSION_CODE >> 8 & 0xf;
91 version[2] = LINUX_VERSION_CODE & 0xf;
92
93 if (json_output) {
94 jsonw_start_object(json_wtr);
95 jsonw_name(json_wtr, "version");
96 jsonw_printf(json_wtr, "\"%u.%u.%u\"",
97 version[0], version[1], version[2]);
98 jsonw_end_object(json_wtr);
99 } else {
100 printf("%s v%u.%u.%u\n", bin_name,
101 version[0], version[1], version[2]);
102 }
Quentin Monnet821cfbb2017-10-19 15:46:26 -0700103 return 0;
104}
105
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700106int cmd_select(const struct cmd *cmds, int argc, char **argv,
107 int (*help)(int argc, char **argv))
108{
109 unsigned int i;
110
111 last_argc = argc;
112 last_argv = argv;
113 last_do_help = help;
114
115 if (argc < 1 && cmds[0].func)
116 return cmds[0].func(argc, argv);
117
118 for (i = 0; cmds[i].func; i++)
119 if (is_prefix(*argv, cmds[i].cmd))
120 return cmds[i].func(argc - 1, argv + 1);
121
122 help(argc - 1, argv + 1);
123
124 return -1;
125}
126
127bool is_prefix(const char *pfx, const char *str)
128{
129 if (!pfx)
130 return false;
131 if (strlen(str) < strlen(pfx))
132 return false;
133
134 return !memcmp(str, pfx, strlen(pfx));
135}
136
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700137void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700138{
139 unsigned char *data = arg;
140 unsigned int i;
141
142 for (i = 0; i < n; i++) {
143 const char *pfx = "";
144
145 if (!i)
146 /* nothing */;
147 else if (!(i % 16))
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700148 fprintf(f, "\n");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700149 else if (!(i % 8))
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700150 fprintf(f, " ");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700151 else
152 pfx = sep;
153
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700154 fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700155 }
156}
157
158static int do_batch(int argc, char **argv);
159
160static const struct cmd cmds[] = {
161 { "help", do_help },
162 { "batch", do_batch },
163 { "prog", do_prog },
164 { "map", do_map },
Quentin Monnet821cfbb2017-10-19 15:46:26 -0700165 { "version", do_version },
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700166 { 0 }
167};
168
169static int do_batch(int argc, char **argv)
170{
171 unsigned int lines = 0;
172 char *n_argv[4096];
173 char buf[65536];
174 int n_argc;
175 FILE *fp;
176 int err;
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700177 int i;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700178
179 if (argc < 2) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700180 p_err("too few parameters for batch");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700181 return -1;
182 } else if (!is_prefix(*argv, "file")) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700183 p_err("expected 'file', got: %s", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700184 return -1;
185 } else if (argc > 2) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700186 p_err("too many parameters for batch");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700187 return -1;
188 }
189 NEXT_ARG();
190
191 fp = fopen(*argv, "r");
192 if (!fp) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700193 p_err("Can't open file (%s): %s", *argv, strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700194 return -1;
195 }
196
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700197 if (json_output)
198 jsonw_start_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700199 while (fgets(buf, sizeof(buf), fp)) {
200 if (strlen(buf) == sizeof(buf) - 1) {
201 errno = E2BIG;
202 break;
203 }
204
205 n_argc = 0;
206 n_argv[n_argc] = strtok(buf, " \t\n");
207
208 while (n_argv[n_argc]) {
209 n_argc++;
210 if (n_argc == ARRAY_SIZE(n_argv)) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700211 p_err("line %d has too many arguments, skip",
212 lines);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700213 n_argc = 0;
214 break;
215 }
216 n_argv[n_argc] = strtok(NULL, " \t\n");
217 }
218
219 if (!n_argc)
220 continue;
221
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700222 if (json_output) {
223 jsonw_start_object(json_wtr);
224 jsonw_name(json_wtr, "command");
225 jsonw_start_array(json_wtr);
226 for (i = 0; i < n_argc; i++)
227 jsonw_string(json_wtr, n_argv[i]);
228 jsonw_end_array(json_wtr);
229 jsonw_name(json_wtr, "output");
230 }
231
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700232 err = cmd_select(cmds, n_argc, n_argv, do_help);
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700233
234 if (json_output)
235 jsonw_end_object(json_wtr);
236
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700237 if (err)
238 goto err_close;
239
240 lines++;
241 }
242
243 if (errno && errno != ENOENT) {
244 perror("reading batch file failed");
245 err = -1;
246 } else {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700247 p_info("processed %d lines", lines);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700248 err = 0;
249 }
250err_close:
251 fclose(fp);
252
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700253 if (json_output)
254 jsonw_end_array(json_wtr);
255
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700256 return err;
257}
258
259int main(int argc, char **argv)
260{
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700261 static const struct option options[] = {
Quentin Monnetd35efba2017-10-23 09:24:07 -0700262 { "json", no_argument, NULL, 'j' },
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700263 { "help", no_argument, NULL, 'h' },
Quentin Monnetd35efba2017-10-23 09:24:07 -0700264 { "pretty", no_argument, NULL, 'p' },
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700265 { "version", no_argument, NULL, 'V' },
266 { 0 }
267 };
Quentin Monnetd35efba2017-10-23 09:24:07 -0700268 int opt, ret;
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700269
270 last_do_help = do_help;
Quentin Monnetd35efba2017-10-23 09:24:07 -0700271 pretty_output = false;
272 json_output = false;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700273 bin_name = argv[0];
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700274
Quentin Monnetd35efba2017-10-23 09:24:07 -0700275 while ((opt = getopt_long(argc, argv, "Vhpj",
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700276 options, NULL)) >= 0) {
277 switch (opt) {
278 case 'V':
279 return do_version(argc, argv);
280 case 'h':
281 return do_help(argc, argv);
Quentin Monnetd35efba2017-10-23 09:24:07 -0700282 case 'p':
283 pretty_output = true;
284 /* fall through */
285 case 'j':
286 json_output = true;
287 break;
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700288 default:
289 usage();
290 }
291 }
292
293 argc -= optind;
294 argv += optind;
295 if (argc < 0)
296 usage();
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700297
Quentin Monnetd35efba2017-10-23 09:24:07 -0700298 if (json_output) {
299 json_wtr = jsonw_new(stdout);
300 if (!json_wtr) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700301 p_err("failed to create JSON writer");
Quentin Monnetd35efba2017-10-23 09:24:07 -0700302 return -1;
303 }
304 jsonw_pretty(json_wtr, pretty_output);
305 }
306
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700307 bfd_init();
308
Quentin Monnetd35efba2017-10-23 09:24:07 -0700309 ret = cmd_select(cmds, argc, argv, do_help);
310
311 if (json_output)
312 jsonw_destroy(&json_wtr);
313
314 return ret;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700315}