]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/q_hhf.c
tc: B.W limits can now be specified in %.
[mirror_iproute2.git] / tc / q_hhf.c
CommitLineData
ac74bd2a
TL
1/* q_hhf.c Heavy-Hitter Filter (HHF)
2 *
3 * Copyright (C) 2013 Terry Lam <vtlam@google.com>
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
ac74bd2a
TL
8#include <fcntl.h>
9#include <sys/socket.h>
10#include <netinet/in.h>
11#include <arpa/inet.h>
12#include <string.h>
13
14#include "utils.h"
15#include "tc_util.h"
16
17static void explain(void)
18{
19 fprintf(stderr, "Usage: ... hhf [ limit PACKETS ] [ quantum BYTES]\n");
20 fprintf(stderr, " [ hh_limit NUMBER ]\n");
21 fprintf(stderr, " [ reset_timeout TIME ]\n");
22 fprintf(stderr, " [ admit_bytes BYTES ]\n");
23 fprintf(stderr, " [ evict_timeout TIME ]\n");
24 fprintf(stderr, " [ non_hh_weight NUMBER ]\n");
25}
26
27static int hhf_parse_opt(struct qdisc_util *qu, int argc, char **argv,
927e3cfb 28 struct nlmsghdr *n, const char *dev)
ac74bd2a 29{
32a121cb
SH
30 unsigned int limit = 0;
31 unsigned int quantum = 0;
32 unsigned int hh_limit = 0;
33 unsigned int reset_timeout = 0;
34 unsigned int admit_bytes = 0;
35 unsigned int evict_timeout = 0;
36 unsigned int non_hh_weight = 0;
ac74bd2a
TL
37 struct rtattr *tail;
38
39 while (argc > 0) {
40 if (strcmp(*argv, "limit") == 0) {
41 NEXT_ARG();
42 if (get_unsigned(&limit, *argv, 0)) {
43 fprintf(stderr, "Illegal \"limit\"\n");
44 return -1;
45 }
46 } else if (strcmp(*argv, "quantum") == 0) {
47 NEXT_ARG();
48 if (get_unsigned(&quantum, *argv, 0)) {
49 fprintf(stderr, "Illegal \"quantum\"\n");
50 return -1;
51 }
52 } else if (strcmp(*argv, "hh_limit") == 0) {
53 NEXT_ARG();
54 if (get_unsigned(&hh_limit, *argv, 0)) {
55 fprintf(stderr, "Illegal \"hh_limit\"\n");
56 return -1;
57 }
58 } else if (strcmp(*argv, "reset_timeout") == 0) {
59 NEXT_ARG();
60 if (get_time(&reset_timeout, *argv)) {
61 fprintf(stderr, "Illegal \"reset_timeout\"\n");
62 return -1;
63 }
64 } else if (strcmp(*argv, "admit_bytes") == 0) {
65 NEXT_ARG();
66 if (get_unsigned(&admit_bytes, *argv, 0)) {
67 fprintf(stderr, "Illegal \"admit_bytes\"\n");
68 return -1;
69 }
70 } else if (strcmp(*argv, "evict_timeout") == 0) {
71 NEXT_ARG();
72 if (get_time(&evict_timeout, *argv)) {
73 fprintf(stderr, "Illegal \"evict_timeout\"\n");
74 return -1;
75 }
76 } else if (strcmp(*argv, "non_hh_weight") == 0) {
77 NEXT_ARG();
78 if (get_unsigned(&non_hh_weight, *argv, 0)) {
79 fprintf(stderr, "Illegal \"non_hh_weight\"\n");
80 return -1;
81 }
82 } else if (strcmp(*argv, "help") == 0) {
83 explain();
84 return -1;
85 } else {
86 fprintf(stderr, "What is \"%s\"?\n", *argv);
87 explain();
88 return -1;
89 }
90 argc--; argv++;
91 }
92
93 tail = NLMSG_TAIL(n);
94 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
95 if (limit)
96 addattr_l(n, 1024, TCA_HHF_BACKLOG_LIMIT, &limit,
97 sizeof(limit));
98 if (quantum)
99 addattr_l(n, 1024, TCA_HHF_QUANTUM, &quantum, sizeof(quantum));
100 if (hh_limit)
101 addattr_l(n, 1024, TCA_HHF_HH_FLOWS_LIMIT, &hh_limit,
102 sizeof(hh_limit));
103 if (reset_timeout)
104 addattr_l(n, 1024, TCA_HHF_RESET_TIMEOUT, &reset_timeout,
105 sizeof(reset_timeout));
106 if (admit_bytes)
107 addattr_l(n, 1024, TCA_HHF_ADMIT_BYTES, &admit_bytes,
108 sizeof(admit_bytes));
109 if (evict_timeout)
110 addattr_l(n, 1024, TCA_HHF_EVICT_TIMEOUT, &evict_timeout,
111 sizeof(evict_timeout));
112 if (non_hh_weight)
113 addattr_l(n, 1024, TCA_HHF_NON_HH_WEIGHT, &non_hh_weight,
114 sizeof(non_hh_weight));
115 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
116 return 0;
117}
118
119static int hhf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
120{
121 struct rtattr *tb[TCA_HHF_MAX + 1];
32a121cb
SH
122 unsigned int limit;
123 unsigned int quantum;
124 unsigned int hh_limit;
125 unsigned int reset_timeout;
126 unsigned int admit_bytes;
127 unsigned int evict_timeout;
128 unsigned int non_hh_weight;
129
ac74bd2a
TL
130 SPRINT_BUF(b1);
131
132 if (opt == NULL)
133 return 0;
134
135 parse_rtattr_nested(tb, TCA_HHF_MAX, opt);
136
137 if (tb[TCA_HHF_BACKLOG_LIMIT] &&
138 RTA_PAYLOAD(tb[TCA_HHF_BACKLOG_LIMIT]) >= sizeof(__u32)) {
139 limit = rta_getattr_u32(tb[TCA_HHF_BACKLOG_LIMIT]);
140 fprintf(f, "limit %up ", limit);
141 }
142 if (tb[TCA_HHF_QUANTUM] &&
143 RTA_PAYLOAD(tb[TCA_HHF_QUANTUM]) >= sizeof(__u32)) {
144 quantum = rta_getattr_u32(tb[TCA_HHF_QUANTUM]);
145 fprintf(f, "quantum %u ", quantum);
146 }
147 if (tb[TCA_HHF_HH_FLOWS_LIMIT] &&
148 RTA_PAYLOAD(tb[TCA_HHF_HH_FLOWS_LIMIT]) >= sizeof(__u32)) {
149 hh_limit = rta_getattr_u32(tb[TCA_HHF_HH_FLOWS_LIMIT]);
150 fprintf(f, "hh_limit %u ", hh_limit);
151 }
152 if (tb[TCA_HHF_RESET_TIMEOUT] &&
153 RTA_PAYLOAD(tb[TCA_HHF_RESET_TIMEOUT]) >= sizeof(__u32)) {
154 reset_timeout = rta_getattr_u32(tb[TCA_HHF_RESET_TIMEOUT]);
155 fprintf(f, "reset_timeout %s ", sprint_time(reset_timeout, b1));
156 }
157 if (tb[TCA_HHF_ADMIT_BYTES] &&
158 RTA_PAYLOAD(tb[TCA_HHF_ADMIT_BYTES]) >= sizeof(__u32)) {
159 admit_bytes = rta_getattr_u32(tb[TCA_HHF_ADMIT_BYTES]);
160 fprintf(f, "admit_bytes %u ", admit_bytes);
161 }
162 if (tb[TCA_HHF_EVICT_TIMEOUT] &&
163 RTA_PAYLOAD(tb[TCA_HHF_EVICT_TIMEOUT]) >= sizeof(__u32)) {
164 evict_timeout = rta_getattr_u32(tb[TCA_HHF_EVICT_TIMEOUT]);
165 fprintf(f, "evict_timeout %s ", sprint_time(evict_timeout, b1));
166 }
167 if (tb[TCA_HHF_NON_HH_WEIGHT] &&
168 RTA_PAYLOAD(tb[TCA_HHF_NON_HH_WEIGHT]) >= sizeof(__u32)) {
169 non_hh_weight = rta_getattr_u32(tb[TCA_HHF_NON_HH_WEIGHT]);
170 fprintf(f, "non_hh_weight %u ", non_hh_weight);
171 }
172 return 0;
173}
174
175static int hhf_print_xstats(struct qdisc_util *qu, FILE *f,
176 struct rtattr *xstats)
177{
178 struct tc_hhf_xstats *st;
179
180 if (xstats == NULL)
181 return 0;
182
183 if (RTA_PAYLOAD(xstats) < sizeof(*st))
184 return -1;
185
186 st = RTA_DATA(xstats);
187
188 fprintf(f, " drop_overlimit %u hh_overlimit %u tot_hh %u cur_hh %u",
189 st->drop_overlimit, st->hh_overlimit,
190 st->hh_tot_count, st->hh_cur_count);
191 return 0;
192}
193
194struct qdisc_util hhf_qdisc_util = {
195 .id = "hhf",
196 .parse_qopt = hhf_parse_opt,
197 .print_qopt = hhf_print_opt,
198 .print_xstats = hhf_print_xstats,
199};