Blame bridge/bridge.c

Packit Service 3880ab
/* SPDX-License-Identifier: GPL-2.0 */
Packit Service 3880ab
/*
Packit Service 3880ab
 * Get/set/delete bridge with netlink
Packit Service 3880ab
 *
Packit Service 3880ab
 * Authors:	Stephen Hemminger <shemminger@vyatta.com>
Packit Service 3880ab
 */
Packit Service 3880ab
Packit Service 3880ab
#include <stdio.h>
Packit Service 3880ab
#include <stdlib.h>
Packit Service 3880ab
#include <unistd.h>
Packit Service 3880ab
#include <sys/socket.h>
Packit Service 3880ab
#include <string.h>
Packit Service 3880ab
#include <errno.h>
Packit Service 3880ab
Packit Service 3880ab
#include "version.h"
Packit Service 3880ab
#include "utils.h"
Packit Service 3880ab
#include "br_common.h"
Packit Service 3880ab
#include "namespace.h"
Packit Service 3880ab
#include "color.h"
Packit Service 3880ab
Packit Service 3880ab
struct rtnl_handle rth = { .fd = -1 };
Packit Service 3880ab
int preferred_family = AF_UNSPEC;
Packit Service 3880ab
int oneline;
Packit Service 3880ab
int show_stats;
Packit Service 3880ab
int show_details;
Packit Service 3880ab
static int color;
Packit Service 3880ab
int compress_vlans;
Packit Service 3880ab
int json;
Packit Service 3880ab
int timestamp;
Packit Service 3880ab
static const char *batch_file;
Packit Service 3880ab
int force;
Packit Service 3880ab
Packit Service 3880ab
static void usage(void) __attribute__((noreturn));
Packit Service 3880ab
Packit Service 3880ab
static void usage(void)
Packit Service 3880ab
{
Packit Service 3880ab
	fprintf(stderr,
Packit Service 3880ab
"Usage: bridge [ OPTIONS ] OBJECT { COMMAND | help }\n"
Packit Service 3880ab
"       bridge [ -force ] -batch filename\n"
Packit Service 3880ab
"where	OBJECT := { link | fdb | mdb | vlan | monitor }\n"
Packit Service 3880ab
"	OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] |\n"
Packit Service 3880ab
"		     -o[neline] | -t[imestamp] | -n[etns] name |\n"
Packit Service 3880ab
"		     -c[ompressvlans] -color -p[retty] -j[son] }\n");
Packit Service 3880ab
	exit(-1);
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int do_help(int argc, char **argv)
Packit Service 3880ab
{
Packit Service 3880ab
	usage();
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
Packit Service 3880ab
static const struct cmd {
Packit Service 3880ab
	const char *cmd;
Packit Service 3880ab
	int (*func)(int argc, char **argv);
Packit Service 3880ab
} cmds[] = {
Packit Service 3880ab
	{ "link",	do_link },
Packit Service 3880ab
	{ "fdb",	do_fdb },
Packit Service 3880ab
	{ "mdb",	do_mdb },
Packit Service 3880ab
	{ "vlan",	do_vlan },
Packit Service 3880ab
	{ "monitor",	do_monitor },
Packit Service 3880ab
	{ "help",	do_help },
Packit Service 3880ab
	{ 0 }
Packit Service 3880ab
};
Packit Service 3880ab
Packit Service 3880ab
static int do_cmd(const char *argv0, int argc, char **argv)
Packit Service 3880ab
{
Packit Service 3880ab
	const struct cmd *c;
Packit Service 3880ab
Packit Service 3880ab
	for (c = cmds; c->cmd; ++c) {
Packit Service 3880ab
		if (matches(argv0, c->cmd) == 0)
Packit Service 3880ab
			return c->func(argc-1, argv+1);
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	fprintf(stderr,
Packit Service 3880ab
		"Object \"%s\" is unknown, try \"bridge help\".\n", argv0);
Packit Service 3880ab
	return -1;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Bot 867fae
static int br_batch_cmd(int argc, char *argv[], void *data)
Packit Bot 867fae
{
Packit Bot 867fae
	return do_cmd(argv[0], argc, argv);
Packit Bot 867fae
}
Packit Bot 867fae
Packit Service 3880ab
static int batch(const char *name)
Packit Service 3880ab
{
Packit Bot 867fae
	int ret;
Packit Service 3880ab
Packit Service 3880ab
	if (rtnl_open(&rth, 0) < 0) {
Packit Service 3880ab
		fprintf(stderr, "Cannot open rtnetlink\n");
Packit Service 3880ab
		return EXIT_FAILURE;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	rtnl_set_strict_dump(&rth);
Packit Service 3880ab
Packit Bot 867fae
	ret = do_batch(name, force, br_batch_cmd, NULL);
Packit Service 3880ab
Packit Service 3880ab
	rtnl_close(&rth);
Packit Service 3880ab
	return ret;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
int
Packit Service 3880ab
main(int argc, char **argv)
Packit Service 3880ab
{
Packit Service 3880ab
	while (argc > 1) {
Packit Service 3880ab
		const char *opt = argv[1];
Packit Service 3880ab
Packit Service 3880ab
		if (strcmp(opt, "--") == 0) {
Packit Service 3880ab
			argc--; argv++;
Packit Service 3880ab
			break;
Packit Service 3880ab
		}
Packit Service 3880ab
		if (opt[0] != '-')
Packit Service 3880ab
			break;
Packit Service 3880ab
		if (opt[1] == '-')
Packit Service 3880ab
			opt++;
Packit Service 3880ab
Packit Service 3880ab
		if (matches(opt, "-help") == 0) {
Packit Service 3880ab
			usage();
Packit Service 3880ab
		} else if (matches(opt, "-Version") == 0) {
Packit Bot 867fae
			printf("bridge utility, %s\n", version);
Packit Service 3880ab
			exit(0);
Packit Service 3880ab
		} else if (matches(opt, "-stats") == 0 ||
Packit Service 3880ab
			   matches(opt, "-statistics") == 0) {
Packit Service 3880ab
			++show_stats;
Packit Service 3880ab
		} else if (matches(opt, "-details") == 0) {
Packit Service 3880ab
			++show_details;
Packit Service 3880ab
		} else if (matches(opt, "-oneline") == 0) {
Packit Service 3880ab
			++oneline;
Packit Service 3880ab
		} else if (matches(opt, "-timestamp") == 0) {
Packit Service 3880ab
			++timestamp;
Packit Service 3880ab
		} else if (matches(opt, "-family") == 0) {
Packit Service 3880ab
			argc--;
Packit Service 3880ab
			argv++;
Packit Service 3880ab
			if (argc <= 1)
Packit Service 3880ab
				usage();
Packit Service 3880ab
			if (strcmp(argv[1], "inet") == 0)
Packit Service 3880ab
				preferred_family = AF_INET;
Packit Service 3880ab
			else if (strcmp(argv[1], "inet6") == 0)
Packit Service 3880ab
				preferred_family = AF_INET6;
Packit Service 3880ab
			else if (strcmp(argv[1], "help") == 0)
Packit Service 3880ab
				usage();
Packit Service 3880ab
			else
Packit Service 3880ab
				invarg("invalid protocol family", argv[1]);
Packit Service 3880ab
		} else if (strcmp(opt, "-4") == 0) {
Packit Service 3880ab
			preferred_family = AF_INET;
Packit Service 3880ab
		} else if (strcmp(opt, "-6") == 0) {
Packit Service 3880ab
			preferred_family = AF_INET6;
Packit Service 3880ab
		} else if (matches(opt, "-netns") == 0) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (netns_switch(argv[1]))
Packit Service 3880ab
				exit(-1);
Packit Service 3880ab
		} else if (matches(opt, "-compressvlans") == 0) {
Packit Service 3880ab
			++compress_vlans;
Packit Service 3880ab
		} else if (matches_color(opt, &color)) {
Packit Service 3880ab
		} else if (matches(opt, "-force") == 0) {
Packit Service 3880ab
			++force;
Packit Service 3880ab
		} else if (matches(opt, "-json") == 0) {
Packit Service 3880ab
			++json;
Packit Service 3880ab
		} else if (matches(opt, "-pretty") == 0) {
Packit Service 3880ab
			++pretty;
Packit Service 3880ab
		} else if (matches(opt, "-batch") == 0) {
Packit Service 3880ab
			argc--;
Packit Service 3880ab
			argv++;
Packit Service 3880ab
			if (argc <= 1)
Packit Service 3880ab
				usage();
Packit Service 3880ab
			batch_file = argv[1];
Packit Service 3880ab
		} else {
Packit Service 3880ab
			fprintf(stderr,
Packit Service 3880ab
				"Option \"%s\" is unknown, try \"bridge help\".\n",
Packit Service 3880ab
				opt);
Packit Service 3880ab
			exit(-1);
Packit Service 3880ab
		}
Packit Service 3880ab
		argc--;	argv++;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	_SL_ = oneline ? "\\" : "\n";
Packit Service 3880ab
Packit Service 3880ab
	check_enable_color(color, json);
Packit Service 3880ab
Packit Service 3880ab
	if (batch_file)
Packit Service 3880ab
		return batch(batch_file);
Packit Service 3880ab
Packit Service 3880ab
	if (rtnl_open(&rth, 0) < 0)
Packit Service 3880ab
		exit(1);
Packit Service 3880ab
Packit Service 3880ab
	rtnl_set_strict_dump(&rth);
Packit Service 3880ab
Packit Service 3880ab
	if (argc > 1)
Packit Service 3880ab
		return do_cmd(argv[1], argc-1, argv+1);
Packit Service 3880ab
Packit Service 3880ab
	rtnl_close(&rth);
Packit Service 3880ab
	usage();
Packit Service 3880ab
}