3178a9887ab66307b6222f8cd03f0640ceae2d39
[platform/upstream/libnl3.git] / src / nl-link-set.c
1 /*
2  * src/nl-link-set.c     Set link attributes
3  *
4  *      This library is free software; you can redistribute it and/or
5  *      modify it under the terms of the GNU Lesser General Public
6  *      License as published by the Free Software Foundation version 2.1
7  *      of the License.
8  *
9  * Copyright (c) 2003-2010 Thomas Graf <tgraf@suug.ch>
10  */
11
12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/link.h>
14
15 static struct nl_sock *sock;
16 static int quiet = 0;
17
18 #if 0
19         "  changes := [link LINK]\n"
20         "             [master MASTER] [qdisc QDISC] [addr ADDR] [broadcast BRD]\n"
21         "             [{ up | down }] [{ arp | noarp }] [{ promisc | nopromisc }]\n"
22         "             [{ dynamic | nodynamic }] [{ multicast | nomulticast }]\n"
23         "             [{ trailers | notrailers }] [{ allmulticast | noallmulticast }]\n");
24 #endif
25
26 static void print_usage(void)
27 {
28         printf(
29         "Usage: nl-link-set [OPTION]... [LINK]\n"
30         "\n"
31         "Options\n"
32         " -q, --quiet           Do not print informal notifications\n"
33         " -h, --help            Show this help\n"
34         " -v, --version         Show versioning information\n"
35         "\n"
36         "Selecting the Link\n"
37         " -n, --name=NAME       link name\n"
38         " -i, --index           interface index\n"
39         "Change Options\n"
40         "     --rename=NAME     rename interface\n"
41         "     --mtu=NUM         MTU value\n"
42         "     --txqlen=NUM      TX queue length\n"
43         "     --weight=NUM      weight\n"
44         "     --ifalias=NAME    alias name (SNMP IfAlias)\n"
45         );
46         exit(0);
47 }
48
49 static void set_cb(struct nl_object *obj, void *arg)
50 {
51         struct rtnl_link *link = nl_object_priv(obj);
52         struct rtnl_link *change = arg;
53         struct nl_dump_params params = {
54                 .dp_type = NL_DUMP_LINE,
55                 .dp_fd = stdout,
56         };
57         int err;
58
59         if ((err = rtnl_link_change(sock, link, change, 0) < 0))
60                 nl_cli_fatal(err, "Unable to change link: %s",
61                              nl_geterror(err));
62
63         if (!quiet) {
64                 printf("Changed ");
65                 nl_object_dump(OBJ_CAST(link), &params);
66         }
67 }
68
69 int main(int argc, char *argv[])
70 {
71         struct nl_cache *link_cache;
72         struct rtnl_link *link, *change;
73         int ok = 0;
74
75         sock = nl_cli_alloc_socket();
76         nl_cli_connect(sock, NETLINK_ROUTE);
77         link_cache = nl_cli_link_alloc_cache(sock);
78         link = nl_cli_link_alloc();
79         change = nl_cli_link_alloc();
80
81         for (;;) {
82                 int c, optidx = 0;
83                 enum {
84                         ARG_RENAME = 257,
85                         ARG_MTU = 258,
86                         ARG_TXQLEN,
87                         ARG_WEIGHT,
88                         ARG_IFALIAS,
89                 };
90                 static struct option long_opts[] = {
91                         { "quiet", 0, 0, 'q' },
92                         { "help", 0, 0, 'h' },
93                         { "version", 0, 0, 'v' },
94                         { "name", 1, 0, 'n' },
95                         { "index", 1, 0, 'i' },
96                         { "rename", 1, 0, ARG_RENAME },
97                         { "mtu", 1, 0, ARG_MTU },
98                         { "txqlen", 1, 0, ARG_TXQLEN },
99                         { "weight", 1, 0, ARG_WEIGHT },
100                         { "ifalias", 1, 0, ARG_IFALIAS },
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                 }
120         }
121
122         if (!ok)
123                 print_usage();
124
125         nl_cache_foreach_filter(link_cache, OBJ_CAST(link), set_cb, change);
126
127         return 0;
128 }