blob: 55ba0a04c102221697f0843f0d204894a97f7928 [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,
73 "Usage: %s OBJECT { COMMAND | help }\n"
74 " %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"
77 " OBJECT := { prog | map }\n",
Quentin Monnet821cfbb2017-10-19 15:46:26 -070078 bin_name, bin_name, bin_name);
Jakub Kicinski71bb4282017-10-04 20:10:04 -070079
80 return 0;
81}
82
Quentin Monnet821cfbb2017-10-19 15:46:26 -070083static int do_version(int argc, char **argv)
84{
Quentin Monnet004b45c2017-10-23 09:24:14 -070085 unsigned int version[3];
86
87 version[0] = LINUX_VERSION_CODE >> 16;
88 version[1] = LINUX_VERSION_CODE >> 8 & 0xf;
89 version[2] = LINUX_VERSION_CODE & 0xf;
90
91 if (json_output) {
92 jsonw_start_object(json_wtr);
93 jsonw_name(json_wtr, "version");
94 jsonw_printf(json_wtr, "\"%u.%u.%u\"",
95 version[0], version[1], version[2]);
96 jsonw_end_object(json_wtr);
97 } else {
98 printf("%s v%u.%u.%u\n", bin_name,
99 version[0], version[1], version[2]);
100 }
Quentin Monnet821cfbb2017-10-19 15:46:26 -0700101 return 0;
102}
103
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700104int cmd_select(const struct cmd *cmds, int argc, char **argv,
105 int (*help)(int argc, char **argv))
106{
107 unsigned int i;
108
109 last_argc = argc;
110 last_argv = argv;
111 last_do_help = help;
112
113 if (argc < 1 && cmds[0].func)
114 return cmds[0].func(argc, argv);
115
116 for (i = 0; cmds[i].func; i++)
117 if (is_prefix(*argv, cmds[i].cmd))
118 return cmds[i].func(argc - 1, argv + 1);
119
120 help(argc - 1, argv + 1);
121
122 return -1;
123}
124
125bool is_prefix(const char *pfx, const char *str)
126{
127 if (!pfx)
128 return false;
129 if (strlen(str) < strlen(pfx))
130 return false;
131
132 return !memcmp(str, pfx, strlen(pfx));
133}
134
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700135void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700136{
137 unsigned char *data = arg;
138 unsigned int i;
139
140 for (i = 0; i < n; i++) {
141 const char *pfx = "";
142
143 if (!i)
144 /* nothing */;
145 else if (!(i % 16))
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700146 fprintf(f, "\n");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700147 else if (!(i % 8))
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700148 fprintf(f, " ");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700149 else
150 pfx = sep;
151
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700152 fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700153 }
154}
155
156static int do_batch(int argc, char **argv);
157
158static const struct cmd cmds[] = {
159 { "help", do_help },
160 { "batch", do_batch },
161 { "prog", do_prog },
162 { "map", do_map },
Quentin Monnet821cfbb2017-10-19 15:46:26 -0700163 { "version", do_version },
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700164 { 0 }
165};
166
167static int do_batch(int argc, char **argv)
168{
169 unsigned int lines = 0;
170 char *n_argv[4096];
171 char buf[65536];
172 int n_argc;
173 FILE *fp;
174 int err;
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700175 int i;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700176
177 if (argc < 2) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700178 p_err("too few parameters for batch");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700179 return -1;
180 } else if (!is_prefix(*argv, "file")) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700181 p_err("expected 'file', got: %s", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700182 return -1;
183 } else if (argc > 2) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700184 p_err("too many parameters for batch");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700185 return -1;
186 }
187 NEXT_ARG();
188
189 fp = fopen(*argv, "r");
190 if (!fp) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700191 p_err("Can't open file (%s): %s", *argv, strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700192 return -1;
193 }
194
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700195 if (json_output)
196 jsonw_start_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700197 while (fgets(buf, sizeof(buf), fp)) {
198 if (strlen(buf) == sizeof(buf) - 1) {
199 errno = E2BIG;
200 break;
201 }
202
203 n_argc = 0;
204 n_argv[n_argc] = strtok(buf, " \t\n");
205
206 while (n_argv[n_argc]) {
207 n_argc++;
208 if (n_argc == ARRAY_SIZE(n_argv)) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700209 p_err("line %d has too many arguments, skip",
210 lines);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700211 n_argc = 0;
212 break;
213 }
214 n_argv[n_argc] = strtok(NULL, " \t\n");
215 }
216
217 if (!n_argc)
218 continue;
219
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700220 if (json_output) {
221 jsonw_start_object(json_wtr);
222 jsonw_name(json_wtr, "command");
223 jsonw_start_array(json_wtr);
224 for (i = 0; i < n_argc; i++)
225 jsonw_string(json_wtr, n_argv[i]);
226 jsonw_end_array(json_wtr);
227 jsonw_name(json_wtr, "output");
228 }
229
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700230 err = cmd_select(cmds, n_argc, n_argv, do_help);
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700231
232 if (json_output)
233 jsonw_end_object(json_wtr);
234
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700235 if (err)
236 goto err_close;
237
238 lines++;
239 }
240
241 if (errno && errno != ENOENT) {
242 perror("reading batch file failed");
243 err = -1;
244 } else {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700245 p_info("processed %d lines", lines);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700246 err = 0;
247 }
248err_close:
249 fclose(fp);
250
Quentin Monnet3aaca6b2017-10-23 09:24:12 -0700251 if (json_output)
252 jsonw_end_array(json_wtr);
253
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700254 return err;
255}
256
257int main(int argc, char **argv)
258{
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700259 static const struct option options[] = {
Quentin Monnetd35efba2017-10-23 09:24:07 -0700260 { "json", no_argument, NULL, 'j' },
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700261 { "help", no_argument, NULL, 'h' },
Quentin Monnetd35efba2017-10-23 09:24:07 -0700262 { "pretty", no_argument, NULL, 'p' },
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700263 { "version", no_argument, NULL, 'V' },
264 { 0 }
265 };
Quentin Monnetd35efba2017-10-23 09:24:07 -0700266 int opt, ret;
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700267
268 last_do_help = do_help;
Quentin Monnetd35efba2017-10-23 09:24:07 -0700269 pretty_output = false;
270 json_output = false;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700271 bin_name = argv[0];
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700272
Quentin Monnetd35efba2017-10-23 09:24:07 -0700273 while ((opt = getopt_long(argc, argv, "Vhpj",
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700274 options, NULL)) >= 0) {
275 switch (opt) {
276 case 'V':
277 return do_version(argc, argv);
278 case 'h':
279 return do_help(argc, argv);
Quentin Monnetd35efba2017-10-23 09:24:07 -0700280 case 'p':
281 pretty_output = true;
282 /* fall through */
283 case 'j':
284 json_output = true;
285 break;
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700286 default:
287 usage();
288 }
289 }
290
291 argc -= optind;
292 argv += optind;
293 if (argc < 0)
294 usage();
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700295
Quentin Monnetd35efba2017-10-23 09:24:07 -0700296 if (json_output) {
297 json_wtr = jsonw_new(stdout);
298 if (!json_wtr) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700299 p_err("failed to create JSON writer");
Quentin Monnetd35efba2017-10-23 09:24:07 -0700300 return -1;
301 }
302 jsonw_pretty(json_wtr, pretty_output);
303 }
304
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700305 bfd_init();
306
Quentin Monnetd35efba2017-10-23 09:24:07 -0700307 ret = cmd_select(cmds, argc, argv, do_help);
308
309 if (json_output)
310 jsonw_destroy(&json_wtr);
311
312 return ret;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700313}