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