blob: 814d19e1b53f573f30e8e7f6dd8cc2572a2a5f4d [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>
39#include <linux/bpf.h>
Quentin Monnet821cfbb2017-10-19 15:46:26 -070040#include <linux/version.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070041#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44
45#include <bpf.h>
46
47#include "main.h"
48
49const char *bin_name;
50static int last_argc;
51static char **last_argv;
52static int (*last_do_help)(int argc, char **argv);
53
54void usage(void)
55{
56 last_do_help(last_argc - 1, last_argv + 1);
57
58 exit(-1);
59}
60
61static int do_help(int argc, char **argv)
62{
63 fprintf(stderr,
64 "Usage: %s OBJECT { COMMAND | help }\n"
65 " %s batch file FILE\n"
Quentin Monnet821cfbb2017-10-19 15:46:26 -070066 " %s version\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -070067 "\n"
68 " OBJECT := { prog | map }\n",
Quentin Monnet821cfbb2017-10-19 15:46:26 -070069 bin_name, bin_name, bin_name);
Jakub Kicinski71bb4282017-10-04 20:10:04 -070070
71 return 0;
72}
73
Quentin Monnet821cfbb2017-10-19 15:46:26 -070074static int do_version(int argc, char **argv)
75{
76 printf("%s v%d.%d.%d\n", bin_name,
77 LINUX_VERSION_CODE >> 16,
78 LINUX_VERSION_CODE >> 8 & 0xf,
79 LINUX_VERSION_CODE & 0xf);
80 return 0;
81}
82
Jakub Kicinski71bb4282017-10-04 20:10:04 -070083int cmd_select(const struct cmd *cmds, int argc, char **argv,
84 int (*help)(int argc, char **argv))
85{
86 unsigned int i;
87
88 last_argc = argc;
89 last_argv = argv;
90 last_do_help = help;
91
92 if (argc < 1 && cmds[0].func)
93 return cmds[0].func(argc, argv);
94
95 for (i = 0; cmds[i].func; i++)
96 if (is_prefix(*argv, cmds[i].cmd))
97 return cmds[i].func(argc - 1, argv + 1);
98
99 help(argc - 1, argv + 1);
100
101 return -1;
102}
103
104bool is_prefix(const char *pfx, const char *str)
105{
106 if (!pfx)
107 return false;
108 if (strlen(str) < strlen(pfx))
109 return false;
110
111 return !memcmp(str, pfx, strlen(pfx));
112}
113
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700114void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700115{
116 unsigned char *data = arg;
117 unsigned int i;
118
119 for (i = 0; i < n; i++) {
120 const char *pfx = "";
121
122 if (!i)
123 /* nothing */;
124 else if (!(i % 16))
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700125 fprintf(f, "\n");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700126 else if (!(i % 8))
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700127 fprintf(f, " ");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700128 else
129 pfx = sep;
130
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700131 fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700132 }
133}
134
135static int do_batch(int argc, char **argv);
136
137static const struct cmd cmds[] = {
138 { "help", do_help },
139 { "batch", do_batch },
140 { "prog", do_prog },
141 { "map", do_map },
Quentin Monnet821cfbb2017-10-19 15:46:26 -0700142 { "version", do_version },
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700143 { 0 }
144};
145
146static int do_batch(int argc, char **argv)
147{
148 unsigned int lines = 0;
149 char *n_argv[4096];
150 char buf[65536];
151 int n_argc;
152 FILE *fp;
153 int err;
154
155 if (argc < 2) {
156 err("too few parameters for batch\n");
157 return -1;
158 } else if (!is_prefix(*argv, "file")) {
159 err("expected 'file', got: %s\n", *argv);
160 return -1;
161 } else if (argc > 2) {
162 err("too many parameters for batch\n");
163 return -1;
164 }
165 NEXT_ARG();
166
167 fp = fopen(*argv, "r");
168 if (!fp) {
169 err("Can't open file (%s): %s\n", *argv, strerror(errno));
170 return -1;
171 }
172
173 while (fgets(buf, sizeof(buf), fp)) {
174 if (strlen(buf) == sizeof(buf) - 1) {
175 errno = E2BIG;
176 break;
177 }
178
179 n_argc = 0;
180 n_argv[n_argc] = strtok(buf, " \t\n");
181
182 while (n_argv[n_argc]) {
183 n_argc++;
184 if (n_argc == ARRAY_SIZE(n_argv)) {
185 err("line %d has too many arguments, skip\n",
186 lines);
187 n_argc = 0;
188 break;
189 }
190 n_argv[n_argc] = strtok(NULL, " \t\n");
191 }
192
193 if (!n_argc)
194 continue;
195
196 err = cmd_select(cmds, n_argc, n_argv, do_help);
197 if (err)
198 goto err_close;
199
200 lines++;
201 }
202
203 if (errno && errno != ENOENT) {
204 perror("reading batch file failed");
205 err = -1;
206 } else {
207 info("processed %d lines\n", lines);
208 err = 0;
209 }
210err_close:
211 fclose(fp);
212
213 return err;
214}
215
216int main(int argc, char **argv)
217{
218 bin_name = argv[0];
219 NEXT_ARG();
220
221 bfd_init();
222
223 return cmd_select(cmds, argc, argv, do_help);
224}