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