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