libnl  3.3.0
nl-qdisc-add.c
1 /*
2  * src/nl-qdisc-add.c Add Queueing Discipline
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) 2010 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/tc.h>
14 #include <netlink/cli/qdisc.h>
15 #include <netlink/cli/link.h>
16 
17 #include <netlink-private/route/tc-api.h>
18 
19 #include <linux/netlink.h>
20 
21 static int quiet = 0;
22 
23 static void print_usage(void)
24 {
25  printf(
26 "Usage: nl-qdisc-add [OPTIONS]... QDISC [CONFIGURATION]...\n"
27 "\n"
28 "OPTIONS\n"
29 " -q, --quiet Do not print informal notifications.\n"
30 " -h, --help Show this help text.\n"
31 " -v, --version Show versioning information.\n"
32 " --update Update qdisc if it exists.\n"
33 " --replace Replace or update qdisc if it exists.\n"
34 " --update-only Only update qdisc, never create it.\n"
35 " --replace-only Only replace or update qdisc, never create it.\n"
36 " -d, --dev=DEV Network device the qdisc should be attached to.\n"
37 " -i, --id=ID ID of new qdisc (default: auto-generated)r\n"
38 " -p, --parent=ID ID of parent { root | ingress | QDISC-ID }\n"
39 "\n"
40 "CONFIGURATION\n"
41 " -h, --help Show help text of qdisc specific options.\n"
42 "\n"
43 "EXAMPLE\n"
44 " $ nl-qdisc-add --dev=eth1 --parent=root htb --rate=100mbit\n"
45 "\n"
46  );
47  exit(0);
48 }
49 
50 int main(int argc, char *argv[])
51 {
52  struct nl_sock *sock;
53  struct rtnl_qdisc *qdisc;
54  struct rtnl_tc *tc;
55  struct nl_cache *link_cache;
56  struct nl_dump_params dp = {
58  .dp_fd = stdout,
59  };
60  struct nl_cli_tc_module *tm;
61  struct rtnl_tc_ops *ops;
62  int err, flags = NLM_F_CREATE | NLM_F_EXCL;
63  char *kind, *id = NULL;
64 
65  sock = nl_cli_alloc_socket();
66  nl_cli_connect(sock, NETLINK_ROUTE);
67 
68  link_cache = nl_cli_link_alloc_cache(sock);
69 
70  qdisc = nl_cli_qdisc_alloc();
71  tc = (struct rtnl_tc *) qdisc;
72 
73  for (;;) {
74  int c, optidx = 0;
75  enum {
76  ARG_REPLACE = 257,
77  ARG_UPDATE = 258,
78  ARG_REPLACE_ONLY,
79  ARG_UPDATE_ONLY,
80  };
81  static struct option long_opts[] = {
82  { "quiet", 0, 0, 'q' },
83  { "help", 0, 0, 'h' },
84  { "version", 0, 0, 'v' },
85  { "dev", 1, 0, 'd' },
86  { "parent", 1, 0, 'p' },
87  { "id", 1, 0, 'i' },
88  { "replace", 0, 0, ARG_REPLACE },
89  { "update", 0, 0, ARG_UPDATE },
90  { "replace-only", 0, 0, ARG_REPLACE_ONLY },
91  { "update-only", 0, 0, ARG_UPDATE_ONLY },
92  { 0, 0, 0, 0 }
93  };
94 
95  c = getopt_long(argc, argv, "+qhvd:p:i:",
96  long_opts, &optidx);
97  if (c == -1)
98  break;
99 
100  switch (c) {
101  case 'q': quiet = 1; break;
102  case 'h': print_usage(); break;
103  case 'v': nl_cli_print_version(); break;
104  case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
105  case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
106  case 'i': id = strdup(optarg); break;
107  case ARG_UPDATE: flags = NLM_F_CREATE; break;
108  case ARG_REPLACE: flags = NLM_F_CREATE | NLM_F_REPLACE; break;
109  case ARG_UPDATE_ONLY: flags = 0; break;
110  case ARG_REPLACE_ONLY: flags = NLM_F_REPLACE; break;
111  }
112  }
113 
114  if (optind >= argc)
115  print_usage();
116 
117  if (!rtnl_tc_get_ifindex(tc))
118  nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)");
119 
120  if (!rtnl_tc_get_parent(tc))
121  nl_cli_fatal(EINVAL, "You must specify a parent");
122 
123  if (id) {
124  nl_cli_tc_parse_handle(tc, id, 1);
125  free(id);
126  }
127 
128  kind = argv[optind++];
129  rtnl_tc_set_kind(tc, kind);
130 
131  if (!(ops = rtnl_tc_get_ops(tc)))
132  nl_cli_fatal(ENOENT, "Unknown qdisc \"%s\"", kind);
133 
134  if (!(tm = nl_cli_tc_lookup(ops)))
135  nl_cli_fatal(ENOTSUP, "Qdisc type \"%s\" not supported.", kind);
136 
137  tm->tm_parse_argv(tc, argc, argv);
138 
139  if (!quiet) {
140  printf("Adding ");
141  nl_object_dump(OBJ_CAST(qdisc), &dp);
142  }
143 
144  if ((err = rtnl_qdisc_add(sock, qdisc, flags)) < 0)
145  nl_cli_fatal(EINVAL, "Unable to add qdisc: %s", nl_geterror(err));
146 
147  return 0;
148 }
int rtnl_tc_set_kind(struct rtnl_tc *tc, const char *kind)
Define the type of traffic control object.
Definition: tc.c:511
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:38
int rtnl_qdisc_add(struct nl_sock *sk, struct rtnl_qdisc *qdisc, int flags)
Add qdisc.
Definition: qdisc.c:162
Dump all attributes but no statistics.
Definition: types.h:23
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
uint32_t rtnl_tc_get_parent(struct rtnl_tc *tc)
Return parent identifier of a traffic control object.
Definition: tc.c:499
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:71
Dumping parameters.
Definition: types.h:33
int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
Return interface index of traffic control object.
Definition: tc.c:275