libnl  3.4.0
nl-class-add.c
1 /*
2  * src/nl-class-add.c Add/Update/Replace Traffic Class
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/class.h>
16 #include <netlink/cli/link.h>
17 
18 #include <netlink-private/route/tc-api.h>
19 
20 #include <linux/netlink.h>
21 
22 static int quiet = 0;
23 
24 static void print_usage(void)
25 {
26  printf(
27 "Usage: nl-class-add [OPTIONS]... class [CONFIGURATION]...\n"
28 "\n"
29 "OPTIONS\n"
30 " -q, --quiet Do not print informal notifications.\n"
31 " -h, --help Show this help text.\n"
32 " -v, --version Show versioning information.\n"
33 " --update Update class if it exists.\n"
34 " --update-only Only update class, never create it.\n"
35 " -d, --dev=DEV Network device the class should be attached to.\n"
36 " -i, --id=ID ID of new class (default: auto-generated)\n"
37 " -p, --parent=ID ID of parent { root | ingress | class-ID }\n"
38 " --mtu=SIZE Overwrite MTU (default: MTU of network device)\n"
39 " --mpu=SIZE Minimum packet size on the link (default: 0).\n"
40 " --overhead=SIZE Overhead in bytes per packet (default: 0).\n"
41 " --linktype=TYPE Overwrite linktype (default: type of network device)\n"
42 "\n"
43 "CONFIGURATION\n"
44 " -h, --help Show help text of class specific options.\n"
45 "\n"
46 "EXAMPLE\n"
47 " $ nl-class-add --dev=eth1 --parent=root htb --rate=100mbit\n"
48 "\n"
49  );
50  exit(0);
51 }
52 
53 int main(int argc, char *argv[])
54 {
55  struct nl_sock *sock;
56  struct rtnl_class *class;
57  struct rtnl_tc *tc;
58  struct nl_cache *link_cache;
59  struct nl_dump_params dp = {
61  .dp_fd = stdout,
62  };
63  struct nl_cli_tc_module *tm;
64  struct rtnl_tc_ops *ops;
65  int err, flags = NLM_F_CREATE | NLM_F_EXCL;
66  char *kind, *id = NULL;
67 
68  sock = nl_cli_alloc_socket();
69  nl_cli_connect(sock, NETLINK_ROUTE);
70 
71  link_cache = nl_cli_link_alloc_cache(sock);
72 
73  class = nl_cli_class_alloc();
74  tc = (struct rtnl_tc *) class;
75 
76  for (;;) {
77  int c, optidx = 0;
78  enum {
79  ARG_UPDATE = 257,
80  ARG_UPDATE_ONLY = 258,
81  ARG_MTU,
82  ARG_MPU,
83  ARG_OVERHEAD,
84  ARG_LINKTYPE,
85  };
86  static struct option long_opts[] = {
87  { "quiet", 0, 0, 'q' },
88  { "help", 0, 0, 'h' },
89  { "version", 0, 0, 'v' },
90  { "dev", 1, 0, 'd' },
91  { "parent", 1, 0, 'p' },
92  { "id", 1, 0, 'i' },
93  { "update", 0, 0, ARG_UPDATE },
94  { "update-only", 0, 0, ARG_UPDATE_ONLY },
95  { "mtu", 1, 0, ARG_MTU },
96  { "mpu", 1, 0, ARG_MPU },
97  { "overhead", 1, 0, ARG_OVERHEAD },
98  { "linktype", 1, 0, ARG_LINKTYPE },
99  { 0, 0, 0, 0 }
100  };
101 
102  c = getopt_long(argc, argv, "+qhvd:p:i:",
103  long_opts, &optidx);
104  if (c == -1)
105  break;
106 
107  switch (c) {
108  case 'q': quiet = 1; break;
109  case 'h': print_usage(); break;
110  case 'v': nl_cli_print_version(); break;
111  case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
112  case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
113  case 'i': id = strdup(optarg); break;
114  case ARG_UPDATE: flags = NLM_F_CREATE; break;
115  case ARG_UPDATE_ONLY: flags = 0; break;
116  case ARG_MTU: nl_cli_tc_parse_mtu(tc, optarg); break;
117  case ARG_MPU: nl_cli_tc_parse_mpu(tc, optarg); break;
118  case ARG_OVERHEAD: nl_cli_tc_parse_overhead(tc, optarg); break;
119  case ARG_LINKTYPE: nl_cli_tc_parse_linktype(tc, optarg); break;
120  }
121  }
122 
123  if (optind >= argc)
124  print_usage();
125 
126  if (!rtnl_tc_get_ifindex(tc))
127  nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)");
128 
129  if (!rtnl_tc_get_parent(tc))
130  nl_cli_fatal(EINVAL, "You must specify a parent (--parent=XXX)");
131 
132  if (id) {
133  nl_cli_tc_parse_handle(tc, id, 1);
134  free(id);
135  }
136 
137  kind = argv[optind++];
138  rtnl_tc_set_kind(tc, kind);
139 
140  if (!(ops = rtnl_tc_get_ops(tc)))
141  nl_cli_fatal(ENOENT, "Unknown class \"%s\"", kind);
142 
143  if (!(tm = nl_cli_tc_lookup(ops)))
144  nl_cli_fatal(ENOTSUP, "class type \"%s\" not supported.", kind);
145 
146  tm->tm_parse_argv(tc, argc, argv);
147 
148  if (!quiet) {
149  printf("Adding ");
150  nl_object_dump(OBJ_CAST(class), &dp);
151  }
152 
153  if ((err = rtnl_class_add(sock, class, flags)) < 0)
154  nl_cli_fatal(EINVAL, "Unable to add class: %s", nl_geterror(err));
155 
156  return 0;
157 }
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
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
int rtnl_class_add(struct nl_sock *sk, struct rtnl_class *class, int flags)
Add/Update traffic class.
Definition: class.c:171
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
int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
Return interface index of traffic control object.
Definition: tc.c:275