blob: 613e3c75f78a941b743c2e06ba8e948da0a551a4 [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);
54
55void usage(void)
56{
57 last_do_help(last_argc - 1, last_argv + 1);
58
59 exit(-1);
60}
61
62static int do_help(int argc, char **argv)
63{
64 fprintf(stderr,
65 "Usage: %s OBJECT { COMMAND | help }\n"
66 " %s batch file FILE\n"
Quentin Monnet821cfbb2017-10-19 15:46:26 -070067 " %s version\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -070068 "\n"
69 " OBJECT := { prog | map }\n",
Quentin Monnet821cfbb2017-10-19 15:46:26 -070070 bin_name, bin_name, bin_name);
Jakub Kicinski71bb4282017-10-04 20:10:04 -070071
72 return 0;
73}
74
Quentin Monnet821cfbb2017-10-19 15:46:26 -070075static int do_version(int argc, char **argv)
76{
77 printf("%s v%d.%d.%d\n", bin_name,
78 LINUX_VERSION_CODE >> 16,
79 LINUX_VERSION_CODE >> 8 & 0xf,
80 LINUX_VERSION_CODE & 0xf);
81 return 0;
82}
83
Jakub Kicinski71bb4282017-10-04 20:10:04 -070084int cmd_select(const struct cmd *cmds, int argc, char **argv,
85 int (*help)(int argc, char **argv))
86{
87 unsigned int i;
88
89 last_argc = argc;
90 last_argv = argv;
91 last_do_help = help;
92
93 if (argc < 1 && cmds[0].func)
94 return cmds[0].func(argc, argv);
95
96 for (i = 0; cmds[i].func; i++)
97 if (is_prefix(*argv, cmds[i].cmd))
98 return cmds[i].func(argc - 1, argv + 1);
99
100 help(argc - 1, argv + 1);
101
102 return -1;
103}
104
105bool is_prefix(const char *pfx, const char *str)
106{
107 if (!pfx)
108 return false;
109 if (strlen(str) < strlen(pfx))
110 return false;
111
112 return !memcmp(str, pfx, strlen(pfx));
113}
114
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700115void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700116{
117 unsigned char *data = arg;
118 unsigned int i;
119
120 for (i = 0; i < n; i++) {
121 const char *pfx = "";
122
123 if (!i)
124 /* nothing */;
125 else if (!(i % 16))
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700126 fprintf(f, "\n");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700127 else if (!(i % 8))
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700128 fprintf(f, " ");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700129 else
130 pfx = sep;
131
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700132 fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700133 }
134}
135
136static int do_batch(int argc, char **argv);
137
138static const struct cmd cmds[] = {
139 { "help", do_help },
140 { "batch", do_batch },
141 { "prog", do_prog },
142 { "map", do_map },
Quentin Monnet821cfbb2017-10-19 15:46:26 -0700143 { "version", do_version },
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700144 { 0 }
145};
146
147static int do_batch(int argc, char **argv)
148{
149 unsigned int lines = 0;
150 char *n_argv[4096];
151 char buf[65536];
152 int n_argc;
153 FILE *fp;
154 int err;
155
156 if (argc < 2) {
157 err("too few parameters for batch\n");
158 return -1;
159 } else if (!is_prefix(*argv, "file")) {
160 err("expected 'file', got: %s\n", *argv);
161 return -1;
162 } else if (argc > 2) {
163 err("too many parameters for batch\n");
164 return -1;
165 }
166 NEXT_ARG();
167
168 fp = fopen(*argv, "r");
169 if (!fp) {
170 err("Can't open file (%s): %s\n", *argv, strerror(errno));
171 return -1;
172 }
173
174 while (fgets(buf, sizeof(buf), fp)) {
175 if (strlen(buf) == sizeof(buf) - 1) {
176 errno = E2BIG;
177 break;
178 }
179
180 n_argc = 0;
181 n_argv[n_argc] = strtok(buf, " \t\n");
182
183 while (n_argv[n_argc]) {
184 n_argc++;
185 if (n_argc == ARRAY_SIZE(n_argv)) {
186 err("line %d has too many arguments, skip\n",
187 lines);
188 n_argc = 0;
189 break;
190 }
191 n_argv[n_argc] = strtok(NULL, " \t\n");
192 }
193
194 if (!n_argc)
195 continue;
196
197 err = cmd_select(cmds, n_argc, n_argv, do_help);
198 if (err)
199 goto err_close;
200
201 lines++;
202 }
203
204 if (errno && errno != ENOENT) {
205 perror("reading batch file failed");
206 err = -1;
207 } else {
208 info("processed %d lines\n", lines);
209 err = 0;
210 }
211err_close:
212 fclose(fp);
213
214 return err;
215}
216
217int main(int argc, char **argv)
218{
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700219 static const struct option options[] = {
220 { "help", no_argument, NULL, 'h' },
221 { "version", no_argument, NULL, 'V' },
222 { 0 }
223 };
224 int opt;
225
226 last_do_help = do_help;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700227 bin_name = argv[0];
Quentin Monneta2bc2e52017-10-23 09:24:06 -0700228
229 while ((opt = getopt_long(argc, argv, "Vh",
230 options, NULL)) >= 0) {
231 switch (opt) {
232 case 'V':
233 return do_version(argc, argv);
234 case 'h':
235 return do_help(argc, argv);
236 default:
237 usage();
238 }
239 }
240
241 argc -= optind;
242 argv += optind;
243 if (argc < 0)
244 usage();
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700245
246 bfd_init();
247
248 return cmd_select(cmds, argc, argv, do_help);
249}