File: nl-link-set.c

package info (click to toggle)
libnl3 3.4.0-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,120 kB
  • sloc: ansic: 57,494; sh: 4,447; python: 3,702; makefile: 888; yacc: 478; cpp: 454; lex: 210
file content (140 lines) | stat: -rw-r--r-- 3,771 bytes parent folder | download | duplicates (2)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
140
/*
 * src/nl-link-set.c     Set link attributes
 *
 *	This library is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation version 2.1
 *	of the License.
 *
 * Copyright (c) 2003-2010 Thomas Graf <tgraf@suug.ch>
 */

#include <netlink/cli/utils.h>
#include <netlink/cli/link.h>

#include <linux/if.h>
#include <linux/netlink.h>

static struct nl_sock *sock;
static int quiet = 0;

#if 0
	"  changes := [link LINK]\n"
	"             [master MASTER] [qdisc QDISC] [addr ADDR] [broadcast BRD]\n"
	"             [{ up | down }] [{ arp | noarp }] [{ promisc | nopromisc }]\n"
	"             [{ dynamic | nodynamic }] [{ multicast | nomulticast }]\n"
	"             [{ trailers | notrailers }] [{ allmulticast | noallmulticast }]\n");
#endif

static void print_usage(void)
{
	printf(
	"Usage: nl-link-set [OPTION]... [LINK]\n"
	"\n"
	"Options\n"
	" -q, --quiet		Do not print informal notifications\n"
	" -h, --help            Show this help\n"
	" -v, --version         Show versioning information\n"
	"\n"
	"Selecting the Link\n"
	" -n, --name=NAME	link name\n"
	" -i, --index           interface index\n"
	"Change Options\n"
	"     --rename=NAME     rename interface\n"
	"     --mtu=NUM         MTU value\n"
	"     --txqlen=NUM      TX queue length\n"
	"     --weight=NUM      weight\n"
	"     --ifalias=NAME    alias name (SNMP IfAlias)\n"
	"     --state=up/down   set interface up/down\n"
	);
	exit(0);
}

static void set_cb(struct nl_object *obj, void *arg)
{
	struct rtnl_link *link = nl_object_priv(obj);
	struct rtnl_link *change = arg;
	struct nl_dump_params params = {
		.dp_type = NL_DUMP_LINE,
		.dp_fd = stdout,
	};
	int err;

	if ((err = rtnl_link_change(sock, link, change, 0)) < 0)
		nl_cli_fatal(err, "Unable to change link: %s",
			     nl_geterror(err));

	if (!quiet) {
		printf("Changed ");
		nl_object_dump(OBJ_CAST(link), &params);
	}
}

int main(int argc, char *argv[])
{
	struct nl_cache *link_cache;
	struct rtnl_link *link, *change;
	int ok = 0;

	sock = nl_cli_alloc_socket();
	nl_cli_connect(sock, NETLINK_ROUTE);
	link_cache = nl_cli_link_alloc_cache(sock);
	link = nl_cli_link_alloc();
	change = nl_cli_link_alloc();

	for (;;) {
		int c, optidx = 0;
		enum {
			ARG_RENAME = 257,
			ARG_MTU = 258,
			ARG_TXQLEN,
			ARG_WEIGHT,
			ARG_IFALIAS,
			ARG_STATE,
		};
		static struct option long_opts[] = {
			{ "quiet", 0, 0, 'q' },
			{ "help", 0, 0, 'h' },
			{ "version", 0, 0, 'v' },
			{ "name", 1, 0, 'n' },
			{ "index", 1, 0, 'i' },
			{ "rename", 1, 0, ARG_RENAME },
			{ "mtu", 1, 0, ARG_MTU },
			{ "txqlen", 1, 0, ARG_TXQLEN },
			{ "weight", 1, 0, ARG_WEIGHT },
			{ "ifalias", 1, 0, ARG_IFALIAS },
			{ "state", 1, 0, ARG_STATE },
			{ 0, 0, 0, 0 }
		};

		c = getopt_long(argc, argv, "qhvn:i:", long_opts, &optidx);
		if (c == -1)
			break;

		switch (c) {
		case 'q': quiet = 1; break;
		case 'h': print_usage(); break;
		case 'v': nl_cli_print_version(); break;
		case 'n': ok++; nl_cli_link_parse_name(link, optarg); break;
		case 'i': ok++; nl_cli_link_parse_ifindex(link, optarg); break;
		case ARG_RENAME: nl_cli_link_parse_name(change, optarg); break;
		case ARG_MTU: nl_cli_link_parse_mtu(change, optarg); break;
		case ARG_TXQLEN: nl_cli_link_parse_txqlen(change, optarg); break;
		case ARG_WEIGHT: nl_cli_link_parse_weight(change, optarg); break;
		case ARG_IFALIAS: nl_cli_link_parse_ifalias(change, optarg); break;
		case ARG_STATE:
			if(!strcmp(optarg, "up"))
				rtnl_link_set_flags(change, IFF_UP);
			else if(!strcmp(optarg, "down"))
				rtnl_link_unset_flags(change, IFF_UP);
			break;
		}
	}

	if (!ok)
		print_usage();

	nl_cache_foreach_filter(link_cache, OBJ_CAST(link), set_cb, change);

	return 0;
}