main.c 6.9 KB
Newer Older
J
Jakub Kicinski 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*
 * Copyright (C) 2017 Netronome Systems, Inc.
 *
 * This software is dual licensed under the GNU General License Version 2,
 * June 1991 as shown in the file COPYING in the top-level directory of this
 * source tree or the BSD 2-Clause License provided below.  You have the
 * option to license this software under the complete terms of either license.
 *
 * The BSD 2-Clause License:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      1. Redistributions of source code must retain the above
 *         copyright notice, this list of conditions and the following
 *         disclaimer.
 *
 *      2. Redistributions in binary form must reproduce the above
 *         copyright notice, this list of conditions and the following
 *         disclaimer in the documentation and/or other materials
 *         provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* Author: Jakub Kicinski <kubakici@wp.pl> */

#include <bfd.h>
#include <ctype.h>
#include <errno.h>
39
#include <getopt.h>
J
Jakub Kicinski 已提交
40
#include <linux/bpf.h>
41
#include <linux/version.h>
J
Jakub Kicinski 已提交
42 43 44 45 46 47 48 49 50 51 52 53
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <bpf.h>

#include "main.h"

const char *bin_name;
static int last_argc;
static char **last_argv;
static int (*last_do_help)(int argc, char **argv);
54 55 56
json_writer_t *json_wtr;
bool pretty_output;
bool json_output;
57
bool show_pinned;
58 59
struct pinned_obj_table prog_table;
struct pinned_obj_table map_table;
J
Jakub Kicinski 已提交
60 61 62 63 64 65 66 67 68 69

void usage(void)
{
	last_do_help(last_argc - 1, last_argv + 1);

	exit(-1);
}

static int do_help(int argc, char **argv)
{
70 71 72 73 74
	if (json_output) {
		jsonw_null(json_wtr);
		return 0;
	}

J
Jakub Kicinski 已提交
75
	fprintf(stderr,
76
		"Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
J
Jakub Kicinski 已提交
77
		"       %s batch file FILE\n"
78
		"       %s version\n"
J
Jakub Kicinski 已提交
79
		"\n"
80 81 82
		"       OBJECT := { prog | map }\n"
		"       " HELP_SPEC_OPTIONS "\n"
		"",
83
		bin_name, bin_name, bin_name);
J
Jakub Kicinski 已提交
84 85 86 87

	return 0;
}

88 89
static int do_version(int argc, char **argv)
{
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	unsigned int version[3];

	version[0] = LINUX_VERSION_CODE >> 16;
	version[1] = LINUX_VERSION_CODE >> 8 & 0xf;
	version[2] = LINUX_VERSION_CODE & 0xf;

	if (json_output) {
		jsonw_start_object(json_wtr);
		jsonw_name(json_wtr, "version");
		jsonw_printf(json_wtr, "\"%u.%u.%u\"",
			     version[0], version[1], version[2]);
		jsonw_end_object(json_wtr);
	} else {
		printf("%s v%u.%u.%u\n", bin_name,
		       version[0], version[1], version[2]);
	}
106 107 108
	return 0;
}

J
Jakub Kicinski 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
int cmd_select(const struct cmd *cmds, int argc, char **argv,
	       int (*help)(int argc, char **argv))
{
	unsigned int i;

	last_argc = argc;
	last_argv = argv;
	last_do_help = help;

	if (argc < 1 && cmds[0].func)
		return cmds[0].func(argc, argv);

	for (i = 0; cmds[i].func; i++)
		if (is_prefix(*argv, cmds[i].cmd))
			return cmds[i].func(argc - 1, argv + 1);

	help(argc - 1, argv + 1);

	return -1;
}

bool is_prefix(const char *pfx, const char *str)
{
	if (!pfx)
		return false;
	if (strlen(str) < strlen(pfx))
		return false;

	return !memcmp(str, pfx, strlen(pfx));
}

140
void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
J
Jakub Kicinski 已提交
141 142 143 144 145 146 147 148 149 150
{
	unsigned char *data = arg;
	unsigned int i;

	for (i = 0; i < n; i++) {
		const char *pfx = "";

		if (!i)
			/* nothing */;
		else if (!(i % 16))
151
			fprintf(f, "\n");
J
Jakub Kicinski 已提交
152
		else if (!(i % 8))
153
			fprintf(f, "  ");
J
Jakub Kicinski 已提交
154 155 156
		else
			pfx = sep;

157
		fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
J
Jakub Kicinski 已提交
158 159 160 161 162 163 164 165 166 167
	}
}

static int do_batch(int argc, char **argv);

static const struct cmd cmds[] = {
	{ "help",	do_help },
	{ "batch",	do_batch },
	{ "prog",	do_prog },
	{ "map",	do_map },
168
	{ "version",	do_version },
J
Jakub Kicinski 已提交
169 170 171 172 173 174 175 176 177 178 179
	{ 0 }
};

static int do_batch(int argc, char **argv)
{
	unsigned int lines = 0;
	char *n_argv[4096];
	char buf[65536];
	int n_argc;
	FILE *fp;
	int err;
180
	int i;
J
Jakub Kicinski 已提交
181 182

	if (argc < 2) {
183
		p_err("too few parameters for batch");
J
Jakub Kicinski 已提交
184 185
		return -1;
	} else if (!is_prefix(*argv, "file")) {
186
		p_err("expected 'file', got: %s", *argv);
J
Jakub Kicinski 已提交
187 188
		return -1;
	} else if (argc > 2) {
189
		p_err("too many parameters for batch");
J
Jakub Kicinski 已提交
190 191 192 193 194 195
		return -1;
	}
	NEXT_ARG();

	fp = fopen(*argv, "r");
	if (!fp) {
196
		p_err("Can't open file (%s): %s", *argv, strerror(errno));
J
Jakub Kicinski 已提交
197 198 199
		return -1;
	}

200 201
	if (json_output)
		jsonw_start_array(json_wtr);
J
Jakub Kicinski 已提交
202 203 204 205 206 207 208 209 210 211 212 213
	while (fgets(buf, sizeof(buf), fp)) {
		if (strlen(buf) == sizeof(buf) - 1) {
			errno = E2BIG;
			break;
		}

		n_argc = 0;
		n_argv[n_argc] = strtok(buf, " \t\n");

		while (n_argv[n_argc]) {
			n_argc++;
			if (n_argc == ARRAY_SIZE(n_argv)) {
214 215
				p_err("line %d has too many arguments, skip",
				      lines);
J
Jakub Kicinski 已提交
216 217 218 219 220 221 222 223 224
				n_argc = 0;
				break;
			}
			n_argv[n_argc] = strtok(NULL, " \t\n");
		}

		if (!n_argc)
			continue;

225 226 227 228 229 230 231 232 233 234
		if (json_output) {
			jsonw_start_object(json_wtr);
			jsonw_name(json_wtr, "command");
			jsonw_start_array(json_wtr);
			for (i = 0; i < n_argc; i++)
				jsonw_string(json_wtr, n_argv[i]);
			jsonw_end_array(json_wtr);
			jsonw_name(json_wtr, "output");
		}

J
Jakub Kicinski 已提交
235
		err = cmd_select(cmds, n_argc, n_argv, do_help);
236 237 238 239

		if (json_output)
			jsonw_end_object(json_wtr);

J
Jakub Kicinski 已提交
240 241 242 243 244 245 246 247 248 249
		if (err)
			goto err_close;

		lines++;
	}

	if (errno && errno != ENOENT) {
		perror("reading batch file failed");
		err = -1;
	} else {
250
		p_info("processed %d lines", lines);
J
Jakub Kicinski 已提交
251 252 253 254 255
		err = 0;
	}
err_close:
	fclose(fp);

256 257 258
	if (json_output)
		jsonw_end_array(json_wtr);

J
Jakub Kicinski 已提交
259 260 261 262 263
	return err;
}

int main(int argc, char **argv)
{
264
	static const struct option options[] = {
265
		{ "json",	no_argument,	NULL,	'j' },
266
		{ "help",	no_argument,	NULL,	'h' },
267
		{ "pretty",	no_argument,	NULL,	'p' },
268
		{ "version",	no_argument,	NULL,	'V' },
269
		{ "bpffs",	no_argument,	NULL,	'f' },
270 271
		{ 0 }
	};
272
	int opt, ret;
273 274

	last_do_help = do_help;
275 276
	pretty_output = false;
	json_output = false;
277
	show_pinned = false;
J
Jakub Kicinski 已提交
278
	bin_name = argv[0];
279

280 281 282
	hash_init(prog_table.table);
	hash_init(map_table.table);

283
	while ((opt = getopt_long(argc, argv, "Vhpjf",
284 285 286 287 288 289
				  options, NULL)) >= 0) {
		switch (opt) {
		case 'V':
			return do_version(argc, argv);
		case 'h':
			return do_help(argc, argv);
290 291 292 293
		case 'p':
			pretty_output = true;
			/* fall through */
		case 'j':
294 295 296 297 298 299 300 301 302
			if (!json_output) {
				json_wtr = jsonw_new(stdout);
				if (!json_wtr) {
					p_err("failed to create JSON writer");
					return -1;
				}
				json_output = true;
			}
			jsonw_pretty(json_wtr, pretty_output);
303
			break;
304 305 306
		case 'f':
			show_pinned = true;
			break;
307 308 309 310 311 312 313 314 315
		default:
			usage();
		}
	}

	argc -= optind;
	argv += optind;
	if (argc < 0)
		usage();
J
Jakub Kicinski 已提交
316 317 318

	bfd_init();

319 320 321 322 323
	ret = cmd_select(cmds, argc, argv, do_help);

	if (json_output)
		jsonw_destroy(&json_wtr);

324 325 326 327
	if (show_pinned) {
		delete_pinned_obj_table(&prog_table);
		delete_pinned_obj_table(&map_table);
	}
328

329
	return ret;
J
Jakub Kicinski 已提交
330
}