libnl  3.7.0
htb.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2010-2011 Thomas Graf <tgraf@suug.ch>
4  */
5 
6 #include <netlink/cli/utils.h>
7 #include <netlink/cli/tc.h>
8 #include <netlink/route/qdisc/htb.h>
9 
10 static void print_qdisc_usage(void)
11 {
12  printf(
13 "Usage: nl-qdisc-add [...] htb [OPTIONS]...\n"
14 "\n"
15 "OPTIONS\n"
16 " --help Show this help text.\n"
17 " --r2q=DIV Rate to quantum divisor (default: 10)\n"
18 " --default=ID Default class for unclassified traffic.\n"
19 "\n"
20 "EXAMPLE"
21 " # Create htb root qdisc 1: and direct unclassified traffic to class 1:10\n"
22 " nl-qdisc-add --dev=eth1 --parent=root --handle=1: htb --default=10\n");
23 }
24 
25 static void htb_parse_qdisc_argv(struct rtnl_tc *tc, int argc, char **argv)
26 {
27  struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) tc;
28 
29  for (;;) {
30  int c, optidx = 0;
31  enum {
32  ARG_R2Q = 257,
33  ARG_DEFAULT = 258,
34  };
35  static struct option long_opts[] = {
36  { "help", 0, 0, 'h' },
37  { "r2q", 1, 0, ARG_R2Q },
38  { "default", 1, 0, ARG_DEFAULT },
39  { 0, 0, 0, 0 }
40  };
41 
42  c = getopt_long(argc, argv, "hv", long_opts, &optidx);
43  if (c == -1)
44  break;
45 
46  switch (c) {
47  case 'h':
48  print_qdisc_usage();
49  return;
50 
51  case ARG_R2Q:
52  rtnl_htb_set_rate2quantum(qdisc, nl_cli_parse_u32(optarg));
53  break;
54 
55  case ARG_DEFAULT:
56  rtnl_htb_set_defcls(qdisc, nl_cli_parse_u32(optarg));
57  break;
58  }
59  }
60 }
61 
62 static void print_class_usage(void)
63 {
64  printf(
65 "Usage: nl-class-add [...] htb [OPTIONS]...\n"
66 "\n"
67 "OPTIONS\n"
68 " --help Show this help text.\n"
69 " --rate=RATE Rate limit.\n"
70 " --ceil=RATE Rate limit while borrowing (default: equal to --rate).\n"
71 " --prio=PRIO Priority, lower is served first (default: 0).\n"
72 " --quantum=SIZE Amount of bytes to serve at once (default: rate/r2q).\n"
73 " --burst=SIZE Max charge size of rate burst buffer (default: auto).\n"
74 " --cburst=SIZE Max charge size of ceil rate burst buffer (default: auto)\n"
75 "\n"
76 "EXAMPLE"
77 " # Attach class 1:1 to htb qdisc 1: and rate limit it to 20mbit\n"
78 " nl-class-add --dev=eth1 --parent=1: --classid=1:1 htb --rate=20mbit\n");
79 }
80 
81 static void htb_parse_class_argv(struct rtnl_tc *tc, int argc, char **argv)
82 {
83  struct rtnl_class *class = (struct rtnl_class *) tc;
84  long rate;
85 
86  for (;;) {
87  int c, optidx = 0;
88  enum {
89  ARG_RATE = 257,
90  ARG_QUANTUM = 258,
91  ARG_CEIL,
92  ARG_PRIO,
93  ARG_BURST,
94  ARG_CBURST,
95  };
96  static struct option long_opts[] = {
97  { "help", 0, 0, 'h' },
98  { "rate", 1, 0, ARG_RATE },
99  { "quantum", 1, 0, ARG_QUANTUM },
100  { "ceil", 1, 0, ARG_CEIL },
101  { "prio", 1, 0, ARG_PRIO },
102  { "burst", 1, 0, ARG_BURST },
103  { "cburst", 1, 0, ARG_CBURST },
104  { 0, 0, 0, 0 }
105  };
106 
107  c = getopt_long(argc, argv, "h", long_opts, &optidx);
108  if (c == -1)
109  break;
110 
111  switch (c) {
112  case 'h':
113  print_class_usage();
114  return;
115 
116  case ARG_RATE:
117  rate = nl_size2int(optarg);
118  if (rate < 0) {
119  nl_cli_fatal(rate, "Unable to parse htb rate "
120  "\"%s\": Invalid format.", optarg);
121  }
122 
123  rtnl_htb_set_rate(class, rate);
124  break;
125 
126  case ARG_CEIL:
127  rate = nl_size2int(optarg);
128  if (rate < 0) {
129  nl_cli_fatal(rate, "Unable to parse htb ceil rate "
130  "\"%s\": Invalid format.", optarg);
131  }
132 
133  rtnl_htb_set_ceil(class, rate);
134  break;
135 
136  case ARG_PRIO:
137  rtnl_htb_set_prio(class, nl_cli_parse_u32(optarg));
138  break;
139 
140  case ARG_QUANTUM:
141  rate = nl_size2int(optarg);
142  if (rate < 0) {
143  nl_cli_fatal(rate, "Unable to parse quantum "
144  "\"%s\": Invalid format.", optarg);
145  }
146 
147  rtnl_htb_set_quantum(class, rate);
148  break;
149 
150  case ARG_BURST:
151  rate = nl_size2int(optarg);
152  if (rate < 0) {
153  nl_cli_fatal(rate, "Unable to parse burst "
154  "\"%s\": Invalid format.", optarg);
155  }
156 
157  rtnl_htb_set_rbuffer(class, rate);
158  break;
159 
160  case ARG_CBURST:
161  rate = nl_size2int(optarg);
162  if (rate < 0) {
163  nl_cli_fatal(rate, "Unable to parse cburst "
164  "\"%s\": Invalid format.", optarg);
165  }
166 
167  rtnl_htb_set_cbuffer(class, rate);
168  break;
169  }
170  }
171 }
172 
173 static struct nl_cli_tc_module htb_qdisc_module =
174 {
175  .tm_name = "htb",
176  .tm_type = RTNL_TC_TYPE_QDISC,
177  .tm_parse_argv = htb_parse_qdisc_argv,
178 };
179 
180 static struct nl_cli_tc_module htb_class_module =
181 {
182  .tm_name = "htb",
183  .tm_type = RTNL_TC_TYPE_CLASS,
184  .tm_parse_argv = htb_parse_class_argv,
185 };
186 
187 static void __init htb_init(void)
188 {
189  nl_cli_tc_register(&htb_qdisc_module);
190  nl_cli_tc_register(&htb_class_module);
191 }
192 
193 static void __exit htb_exit(void)
194 {
195  nl_cli_tc_unregister(&htb_class_module);
196  nl_cli_tc_unregister(&htb_qdisc_module);
197 }
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:71
uint32_t nl_cli_parse_u32(const char *arg)
Parse a text based 32 bit unsigned integer argument.
Definition: utils.c:36
int rtnl_htb_set_rbuffer(struct rtnl_class *class, uint32_t rbuffer)
Set size of the rate bucket of HTB class.
Definition: htb.c:567
int rtnl_htb_set_defcls(struct rtnl_qdisc *qdisc, uint32_t defcls)
Set default class of the htb qdisc to the specified value.
Definition: htb.c:354
int rtnl_htb_set_quantum(struct rtnl_class *class, uint32_t quantum)
Set quantum of HTB class (overwrites value calculated based on r2q)
Definition: htb.c:645
int rtnl_htb_set_rate(struct rtnl_class *class, uint32_t rate)
Set rate of HTB class.
Definition: htb.c:442
int rtnl_htb_set_ceil(struct rtnl_class *class, uint32_t ceil)
Set ceil rate of HTB class.
Definition: htb.c:518
int rtnl_htb_set_cbuffer(struct rtnl_class *class, uint32_t cbuffer)
Set size of the ceil bucket of HTB class.
Definition: htb.c:603
long nl_size2int(const char *str)
Convert a character string to a size.
Definition: utils.c:288