1 /* 2 * Fair Queue 3 * 4 * Copyright (C) 2013-2015 Eric Dumazet <edumazet (at) google.com> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions, and the following disclaimer, 11 * without modification. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The names of the authors may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * Alternatively, provided that this notice is retained in full, this 19 * software may be distributed under the terms of the GNU General 20 * Public License ("GPL") version 2, in which case the provisions of the 21 * GPL apply INSTEAD OF those given above. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 34 * DAMAGE. 35 * 36 */ 37 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <unistd.h> 41 #include <syslog.h> 42 #include <fcntl.h> 43 #include <sys/socket.h> 44 #include <netinet/in.h> 45 #include <arpa/inet.h> 46 #include <string.h> 47 #include <stdbool.h> 48 49 #include "utils.h" 50 #include "tc_util.h" 51 52 static void explain(void) 53 { 54 fprintf(stderr, "Usage: ... fq [ limit PACKETS ] [ flow_limit PACKETS ]\n"); 55 fprintf(stderr, " [ quantum BYTES ] [ initial_quantum BYTES ]\n"); 56 fprintf(stderr, " [ maxrate RATE ] [ buckets NUMBER ]\n"); 57 fprintf(stderr, " [ [no]pacing ] [ refill_delay TIME ]\n"); 58 fprintf(stderr, " [ low_rate_threshold RATE ]\n"); 59 fprintf(stderr, " [ orphan_mask MASK]\n"); 60 } 61 62 static unsigned int ilog2(unsigned int val) 63 { 64 unsigned int res = 0; 65 66 val--; 67 while (val) { 68 res++; 69 val >>= 1; 70 } 71 return res; 72 } 73 74 static int fq_parse_opt(struct qdisc_util *qu, int argc, char **argv, 75 struct nlmsghdr *n) 76 { 77 unsigned int plimit; 78 unsigned int flow_plimit; 79 unsigned int quantum; 80 unsigned int initial_quantum; 81 unsigned int buckets = 0; 82 unsigned int maxrate; 83 unsigned int low_rate_threshold; 84 unsigned int defrate; 85 unsigned int refill_delay; 86 unsigned int orphan_mask; 87 bool set_plimit = false; 88 bool set_flow_plimit = false; 89 bool set_quantum = false; 90 bool set_initial_quantum = false; 91 bool set_maxrate = false; 92 bool set_defrate = false; 93 bool set_refill_delay = false; 94 bool set_orphan_mask = false; 95 bool set_low_rate_threshold = false; 96 int pacing = -1; 97 struct rtattr *tail; 98 99 while (argc > 0) { 100 if (strcmp(*argv, "limit") == 0) { 101 NEXT_ARG(); 102 if (get_unsigned(&plimit, *argv, 0)) { 103 fprintf(stderr, "Illegal \"limit\"\n"); 104 return -1; 105 } 106 set_plimit = true; 107 } else if (strcmp(*argv, "flow_limit") == 0) { 108 NEXT_ARG(); 109 if (get_unsigned(&flow_plimit, *argv, 0)) { 110 fprintf(stderr, "Illegal \"flow_limit\"\n"); 111 return -1; 112 } 113 set_flow_plimit = true; 114 } else if (strcmp(*argv, "buckets") == 0) { 115 NEXT_ARG(); 116 if (get_unsigned(&buckets, *argv, 0)) { 117 fprintf(stderr, "Illegal \"buckets\"\n"); 118 return -1; 119 } 120 } else if (strcmp(*argv, "maxrate") == 0) { 121 NEXT_ARG(); 122 if (get_rate(&maxrate, *argv)) { 123 fprintf(stderr, "Illegal \"maxrate\"\n"); 124 return -1; 125 } 126 set_maxrate = true; 127 } else if (strcmp(*argv, "low_rate_threshold") == 0) { 128 NEXT_ARG(); 129 if (get_rate(&low_rate_threshold, *argv)) { 130 fprintf(stderr, "Illegal \"low_rate_threshold\"\n"); 131 return -1; 132 } 133 set_low_rate_threshold = true; 134 } else if (strcmp(*argv, "defrate") == 0) { 135 NEXT_ARG(); 136 if (get_rate(&defrate, *argv)) { 137 fprintf(stderr, "Illegal \"defrate\"\n"); 138 return -1; 139 } 140 set_defrate = true; 141 } else if (strcmp(*argv, "quantum") == 0) { 142 NEXT_ARG(); 143 if (get_unsigned(&quantum, *argv, 0)) { 144 fprintf(stderr, "Illegal \"quantum\"\n"); 145 return -1; 146 } 147 set_quantum = true; 148 } else if (strcmp(*argv, "initial_quantum") == 0) { 149 NEXT_ARG(); 150 if (get_unsigned(&initial_quantum, *argv, 0)) { 151 fprintf(stderr, "Illegal \"initial_quantum\"\n"); 152 return -1; 153 } 154 set_initial_quantum = true; 155 } else if (strcmp(*argv, "orphan_mask") == 0) { 156 NEXT_ARG(); 157 if (get_unsigned(&orphan_mask, *argv, 0)) { 158 fprintf(stderr, "Illegal \"initial_quantum\"\n"); 159 return -1; 160 } 161 set_orphan_mask = true; 162 } else if (strcmp(*argv, "refill_delay") == 0) { 163 NEXT_ARG(); 164 if (get_time(&refill_delay, *argv)) { 165 fprintf(stderr, "Illegal \"refill_delay\"\n"); 166 return -1; 167 } 168 set_refill_delay = true; 169 } else if (strcmp(*argv, "pacing") == 0) { 170 pacing = 1; 171 } else if (strcmp(*argv, "nopacing") == 0) { 172 pacing = 0; 173 } else if (strcmp(*argv, "help") == 0) { 174 explain(); 175 return -1; 176 } else { 177 fprintf(stderr, "What is \"%s\"?\n", *argv); 178 explain(); 179 return -1; 180 } 181 argc--; argv++; 182 } 183 184 tail = NLMSG_TAIL(n); 185 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0); 186 if (buckets) { 187 unsigned int log = ilog2(buckets); 188 189 addattr_l(n, 1024, TCA_FQ_BUCKETS_LOG, 190 &log, sizeof(log)); 191 } 192 if (set_plimit) 193 addattr_l(n, 1024, TCA_FQ_PLIMIT, 194 &plimit, sizeof(plimit)); 195 if (set_flow_plimit) 196 addattr_l(n, 1024, TCA_FQ_FLOW_PLIMIT, 197 &flow_plimit, sizeof(flow_plimit)); 198 if (set_quantum) 199 addattr_l(n, 1024, TCA_FQ_QUANTUM, &quantum, sizeof(quantum)); 200 if (set_initial_quantum) 201 addattr_l(n, 1024, TCA_FQ_INITIAL_QUANTUM, 202 &initial_quantum, sizeof(initial_quantum)); 203 if (pacing != -1) 204 addattr_l(n, 1024, TCA_FQ_RATE_ENABLE, 205 &pacing, sizeof(pacing)); 206 if (set_maxrate) 207 addattr_l(n, 1024, TCA_FQ_FLOW_MAX_RATE, 208 &maxrate, sizeof(maxrate)); 209 if (set_low_rate_threshold) 210 addattr_l(n, 1024, TCA_FQ_LOW_RATE_THRESHOLD, 211 &low_rate_threshold, sizeof(low_rate_threshold)); 212 if (set_defrate) 213 addattr_l(n, 1024, TCA_FQ_FLOW_DEFAULT_RATE, 214 &defrate, sizeof(defrate)); 215 if (set_refill_delay) 216 addattr_l(n, 1024, TCA_FQ_FLOW_REFILL_DELAY, 217 &refill_delay, sizeof(refill_delay)); 218 if (set_orphan_mask) 219 addattr_l(n, 1024, TCA_FQ_ORPHAN_MASK, 220 &orphan_mask, sizeof(refill_delay)); 221 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; 222 return 0; 223 } 224 225 static int fq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) 226 { 227 struct rtattr *tb[TCA_FQ_MAX + 1]; 228 unsigned int plimit, flow_plimit; 229 unsigned int buckets_log; 230 int pacing; 231 unsigned int rate, quantum; 232 unsigned int refill_delay; 233 unsigned int orphan_mask; 234 235 SPRINT_BUF(b1); 236 237 if (opt == NULL) 238 return 0; 239 240 parse_rtattr_nested(tb, TCA_FQ_MAX, opt); 241 242 if (tb[TCA_FQ_PLIMIT] && 243 RTA_PAYLOAD(tb[TCA_FQ_PLIMIT]) >= sizeof(__u32)) { 244 plimit = rta_getattr_u32(tb[TCA_FQ_PLIMIT]); 245 fprintf(f, "limit %up ", plimit); 246 } 247 if (tb[TCA_FQ_FLOW_PLIMIT] && 248 RTA_PAYLOAD(tb[TCA_FQ_FLOW_PLIMIT]) >= sizeof(__u32)) { 249 flow_plimit = rta_getattr_u32(tb[TCA_FQ_FLOW_PLIMIT]); 250 fprintf(f, "flow_limit %up ", flow_plimit); 251 } 252 if (tb[TCA_FQ_BUCKETS_LOG] && 253 RTA_PAYLOAD(tb[TCA_FQ_BUCKETS_LOG]) >= sizeof(__u32)) { 254 buckets_log = rta_getattr_u32(tb[TCA_FQ_BUCKETS_LOG]); 255 fprintf(f, "buckets %u ", 1U << buckets_log); 256 } 257 if (tb[TCA_FQ_ORPHAN_MASK] && 258 RTA_PAYLOAD(tb[TCA_FQ_ORPHAN_MASK]) >= sizeof(__u32)) { 259 orphan_mask = rta_getattr_u32(tb[TCA_FQ_ORPHAN_MASK]); 260 fprintf(f, "orphan_mask %u ", orphan_mask); 261 } 262 if (tb[TCA_FQ_RATE_ENABLE] && 263 RTA_PAYLOAD(tb[TCA_FQ_RATE_ENABLE]) >= sizeof(int)) { 264 pacing = rta_getattr_u32(tb[TCA_FQ_RATE_ENABLE]); 265 if (pacing == 0) 266 fprintf(f, "nopacing "); 267 } 268 if (tb[TCA_FQ_QUANTUM] && 269 RTA_PAYLOAD(tb[TCA_FQ_QUANTUM]) >= sizeof(__u32)) { 270 quantum = rta_getattr_u32(tb[TCA_FQ_QUANTUM]); 271 fprintf(f, "quantum %u ", quantum); 272 } 273 if (tb[TCA_FQ_INITIAL_QUANTUM] && 274 RTA_PAYLOAD(tb[TCA_FQ_INITIAL_QUANTUM]) >= sizeof(__u32)) { 275 quantum = rta_getattr_u32(tb[TCA_FQ_INITIAL_QUANTUM]); 276 fprintf(f, "initial_quantum %u ", quantum); 277 } 278 if (tb[TCA_FQ_FLOW_MAX_RATE] && 279 RTA_PAYLOAD(tb[TCA_FQ_FLOW_MAX_RATE]) >= sizeof(__u32)) { 280 rate = rta_getattr_u32(tb[TCA_FQ_FLOW_MAX_RATE]); 281 282 if (rate != ~0U) 283 fprintf(f, "maxrate %s ", sprint_rate(rate, b1)); 284 } 285 if (tb[TCA_FQ_FLOW_DEFAULT_RATE] && 286 RTA_PAYLOAD(tb[TCA_FQ_FLOW_DEFAULT_RATE]) >= sizeof(__u32)) { 287 rate = rta_getattr_u32(tb[TCA_FQ_FLOW_DEFAULT_RATE]); 288 289 if (rate != 0) 290 fprintf(f, "defrate %s ", sprint_rate(rate, b1)); 291 } 292 if (tb[TCA_FQ_LOW_RATE_THRESHOLD] && 293 RTA_PAYLOAD(tb[TCA_FQ_LOW_RATE_THRESHOLD]) >= sizeof(__u32)) { 294 rate = rta_getattr_u32(tb[TCA_FQ_LOW_RATE_THRESHOLD]); 295 296 if (rate != 0) 297 fprintf(f, "low_rate_threshold %s ", sprint_rate(rate, b1)); 298 } 299 if (tb[TCA_FQ_FLOW_REFILL_DELAY] && 300 RTA_PAYLOAD(tb[TCA_FQ_FLOW_REFILL_DELAY]) >= sizeof(__u32)) { 301 refill_delay = rta_getattr_u32(tb[TCA_FQ_FLOW_REFILL_DELAY]); 302 fprintf(f, "refill_delay %s ", sprint_time(refill_delay, b1)); 303 } 304 305 return 0; 306 } 307 308 static int fq_print_xstats(struct qdisc_util *qu, FILE *f, 309 struct rtattr *xstats) 310 { 311 struct tc_fq_qd_stats *st; 312 313 if (xstats == NULL) 314 return 0; 315 316 if (RTA_PAYLOAD(xstats) < sizeof(*st)) 317 return -1; 318 319 st = RTA_DATA(xstats); 320 321 fprintf(f, " %u flows (%u inactive, %u throttled)", 322 st->flows, st->inactive_flows, st->throttled_flows); 323 324 if (st->time_next_delayed_flow > 0) 325 fprintf(f, ", next packet delay %llu ns", st->time_next_delayed_flow); 326 327 fprintf(f, "\n %llu gc, %llu highprio", 328 st->gc_flows, st->highprio_packets); 329 330 if (st->tcp_retrans) 331 fprintf(f, ", %llu retrans", st->tcp_retrans); 332 333 fprintf(f, ", %llu throttled", st->throttled); 334 335 if (st->unthrottle_latency_ns) 336 fprintf(f, ", %u ns latency", st->unthrottle_latency_ns); 337 338 if (st->flows_plimit) 339 fprintf(f, ", %llu flows_plimit", st->flows_plimit); 340 341 if (st->pkts_too_long || st->allocation_errors) 342 fprintf(f, "\n %llu too long pkts, %llu alloc errors\n", 343 st->pkts_too_long, st->allocation_errors); 344 345 return 0; 346 } 347 348 struct qdisc_util fq_qdisc_util = { 349 .id = "fq", 350 .parse_qopt = fq_parse_opt, 351 .print_qopt = fq_print_opt, 352 .print_xstats = fq_print_xstats, 353 }; 354