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