blob: 94c94e78616062c587254daa405a57a9f348ad0a [file] [log] [blame]
Thomas Graf44d36242007-09-15 01:28:01 +02001/*
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 *
Thomas Graf88087432009-12-16 16:20:46 +01009 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
Thomas Graf44d36242007-09-15 01:28:01 +020010 */
11
Thomas Graf88087432009-12-16 16:20:46 +010012#include <netlink/cli/utils.h>
13#include <netlink/cli/link.h>
Thomas Graf44d36242007-09-15 01:28:01 +020014
Thomas Graf10cf5a52008-05-23 00:02:02 +020015static struct nl_sock *sock;
16static int quiet = 0;
17
18#if 0
19 " changes := [link LINK]\n"
Thomas Graf44d36242007-09-15 01:28:01 +020020 " [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");
Thomas Graf10cf5a52008-05-23 00:02:02 +020024#endif
25
26static 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 );
45 exit(0);
Thomas Graf44d36242007-09-15 01:28:01 +020046}
47
Thomas Graf10cf5a52008-05-23 00:02:02 +020048static void set_cb(struct nl_object *obj, void *arg)
49{
50 struct rtnl_link *link = nl_object_priv(obj);
51 struct rtnl_link *change = arg;
52 struct nl_dump_params params = {
Thomas Grafd8443072008-05-23 23:45:14 +020053 .dp_type = NL_DUMP_LINE,
Thomas Graf10cf5a52008-05-23 00:02:02 +020054 .dp_fd = stdout,
55 };
56 int err;
57
58 if ((err = rtnl_link_change(sock, link, change, 0) < 0))
Thomas Graf88087432009-12-16 16:20:46 +010059 nl_cli_fatal(err, "Unable to change link: %s",
60 nl_geterror(err));
Thomas Graf10cf5a52008-05-23 00:02:02 +020061
62 if (!quiet) {
63 printf("Changed ");
64 nl_object_dump(OBJ_CAST(link), &params);
65 }
66}
Thomas Graf44d36242007-09-15 01:28:01 +020067
68int main(int argc, char *argv[])
69{
Thomas Graf44d36242007-09-15 01:28:01 +020070 struct nl_cache *link_cache;
Thomas Graf10cf5a52008-05-23 00:02:02 +020071 struct rtnl_link *link, *change;
72 int ok = 0;
Thomas Graf44d36242007-09-15 01:28:01 +020073
Thomas Graf88087432009-12-16 16:20:46 +010074 sock = nl_cli_alloc_socket();
75 nl_cli_connect(sock, NETLINK_ROUTE);
76 link_cache = nl_cli_link_alloc_cache(sock);
77 link = nl_cli_link_alloc();
78 change = nl_cli_link_alloc();
Thomas Graf44d36242007-09-15 01:28:01 +020079
Thomas Graf10cf5a52008-05-23 00:02:02 +020080 for (;;) {
81 int c, optidx = 0;
82 enum {
83 ARG_RENAME = 257,
84 ARG_MTU = 258,
85 ARG_TXQLEN,
86 ARG_WEIGHT,
87 };
88 static struct option long_opts[] = {
89 { "quiet", 0, 0, 'q' },
90 { "help", 0, 0, 'h' },
91 { "version", 0, 0, 'v' },
92 { "name", 1, 0, 'n' },
93 { "index", 1, 0, 'i' },
94 { "rename", 1, 0, ARG_RENAME },
95 { "mtu", 1, 0, ARG_MTU },
96 { "txqlen", 1, 0, ARG_TXQLEN },
97 { "weight", 1, 0, ARG_WEIGHT },
98 { 0, 0, 0, 0 }
99 };
100
101 c = getopt_long(argc, argv, "qhvn:i:", long_opts, &optidx);
102 if (c == -1)
103 break;
104
105 switch (c) {
106 case 'q': quiet = 1; break;
107 case 'h': print_usage(); break;
Thomas Graf88087432009-12-16 16:20:46 +0100108 case 'v': nl_cli_print_version(); break;
109 case 'n': ok++; nl_cli_link_parse_name(link, optarg); break;
110 case 'i': ok++; nl_cli_link_parse_ifindex(link, optarg); break;
111 case ARG_RENAME: nl_cli_link_parse_name(change, optarg); break;
112 case ARG_MTU: nl_cli_link_parse_mtu(link, optarg); break;
113 case ARG_TXQLEN: nl_cli_link_parse_txqlen(link, optarg); break;
114 case ARG_WEIGHT: nl_cli_link_parse_weight(link, optarg); break;
Thomas Graf10cf5a52008-05-23 00:02:02 +0200115 }
116 }
117
118 if (!ok)
Thomas Graf44d36242007-09-15 01:28:01 +0200119 print_usage();
120
Thomas Graf10cf5a52008-05-23 00:02:02 +0200121 nl_cache_foreach_filter(link_cache, OBJ_CAST(link), set_cb, change);
Thomas Graf44d36242007-09-15 01:28:01 +0200122
Thomas Graf10cf5a52008-05-23 00:02:02 +0200123 return 0;
Thomas Graf44d36242007-09-15 01:28:01 +0200124}