libnl: /data/src/libnl3/src/nl-link-set.c Source File

libnl 3.7.0

nl-link-set.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2003-2010 Thomas Graf <tgraf@suug.ch>
4 */
5
6 #include <netlink/cli/utils.h>
7 #include <netlink/cli/link.h>
8
9 #include <linux/if.h>
10 #include <linux/netlink.h>
11
12 static struct nl_sock *sock;
13 static int quiet = 0;
14
15 #if 0
16 " changes := [link LINK]\n"
17 " [master MASTER] [qdisc QDISC] [addr ADDR] [broadcast BRD]\n"
18 " [{ up | down }] [{ arp | noarp }] [{ promisc | nopromisc }]\n"
19 " [{ dynamic | nodynamic }] [{ multicast | nomulticast }]\n"
20 " [{ trailers | notrailers }] [{ allmulticast | noallmulticast }]\n");
21 #endif
22
23 static void print_usage(void)
24 {
25 printf(
26 "Usage: nl-link-set [OPTION]... [LINK]\n"
27 "\n"
28 "Options\n"
29 " -q, --quiet Do not print informal notifications\n"
30 " -h, --help Show this help\n"
31 " -v, --version Show versioning information\n"
32 "\n"
33 "Selecting the Link\n"
34 " -n, --name=NAME link name\n"
35 " -i, --index interface index\n"
36 "Change Options\n"
37 " --rename=NAME rename interface\n"
38 " --mtu=NUM MTU value\n"
39 " --txqlen=NUM TX queue length\n"
40 " --weight=NUM weight\n"
41 " --ifalias=NAME alias name (SNMP IfAlias)\n"
42 " --state=up/down set interface up/down\n"
43 );
44 exit(0);
45 }
46
47 static void set_cb(struct nl_object *obj, void *arg)
48 {
49 struct rtnl_link *link = nl_object_priv(obj);
50 struct rtnl_link *change = arg;
51 struct nl_dump_params params = {
52 .dp_type = NL_DUMP_LINE,
53 .dp_fd = stdout,
54 };
55 int err;
56
57 if ((err = rtnl_link_change(sock, link, change, 0)) < 0)
58 nl_cli_fatal(err, "Unable to change link: %s",
59 nl_geterror(err));
60
61 if (!quiet) {
62 printf("Changed ");
63 nl_object_dump(OBJ_CAST(link), &params);
64 }
65 }
66
67 int main(int argc, char *argv[])
68 {
69 struct nl_cache *link_cache;
70 struct rtnl_link *link, *change;
71 int ok = 0;
72
73 sock = nl_cli_alloc_socket();
74 nl_cli_connect(sock, NETLINK_ROUTE);
75 link_cache = nl_cli_link_alloc_cache(sock);
76 link = nl_cli_link_alloc();
77 change = nl_cli_link_alloc();
78
79 for (;;) {
80 int c, optidx = 0;
81 enum {
82 ARG_RENAME = 257,
83 ARG_MTU = 258,
84 ARG_TXQLEN,
85 ARG_WEIGHT,
86 ARG_IFALIAS,
87 ARG_STATE,
88 };
89 static struct option long_opts[] = {
90 { "quiet", 0, 0, 'q' },
91 { "help", 0, 0, 'h' },
92 { "version", 0, 0, 'v' },
93 { "name", 1, 0, 'n' },
94 { "index", 1, 0, 'i' },
95 { "rename", 1, 0, ARG_RENAME },
96 { "mtu", 1, 0, ARG_MTU },
97 { "txqlen", 1, 0, ARG_TXQLEN },
98 { "weight", 1, 0, ARG_WEIGHT },
99 { "ifalias", 1, 0, ARG_IFALIAS },
100 { "state", 1, 0, ARG_STATE },
101 { 0, 0, 0, 0 }
102 };
103
104 c = getopt_long(argc, argv, "qhvn:i:", long_opts, &optidx);
105 if (c == -1)
106 break;
107
108 switch (c) {
109 case 'q': quiet = 1; break;
110 case 'h': print_usage(); break;
111 case 'v': nl_cli_print_version(); break;
112 case 'n': ok++; nl_cli_link_parse_name(link, optarg); break;
113 case 'i': ok++; nl_cli_link_parse_ifindex(link, optarg); break;
114 case ARG_RENAME: nl_cli_link_parse_name(change, optarg); break;
115 case ARG_MTU: nl_cli_link_parse_mtu(change, optarg); break;
116 case ARG_TXQLEN: nl_cli_link_parse_txqlen(change, optarg); break;
117 case ARG_WEIGHT: nl_cli_link_parse_weight(change, optarg); break;
118 case ARG_IFALIAS: nl_cli_link_parse_ifalias(change, optarg); break;
119 case ARG_STATE:
120 if(!strcmp(optarg, "up"))
121 rtnl_link_set_flags(change, IFF_UP);
122 else if(!strcmp(optarg, "down"))
123 rtnl_link_unset_flags(change, IFF_UP);
124 break;
125 }
126 }
127
128 if (!ok)
129 print_usage();
130
131 nl_cache_foreach_filter(link_cache, OBJ_CAST(link), set_cb, change);
132
133 return 0;
134 }
nl_cache_foreach_filter
void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback on each element of the cache (filtered).
Definition: cache.c:1294
nl_cli_fatal
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:71
rtnl_link_unset_flags
void rtnl_link_unset_flags(struct rtnl_link *link, unsigned int flags)
Unset flags of link object.
Definition: link.c:2075
rtnl_link_set_flags
void rtnl_link_set_flags(struct rtnl_link *link, unsigned int flags)
Set flags of link object.
Definition: link.c:2060
rtnl_link_change
int rtnl_link_change(struct nl_sock *sk, struct rtnl_link *orig, struct rtnl_link *changes, int flags)
Change link.
Definition: link.c:1787
nl_object_dump
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:287
NL_DUMP_LINE
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
nl_dump_params
Dumping parameters.
Definition: types.h:28
nl_dump_params::dp_type
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:32
rtnl_link
Link object.