1/* 2 * Fair Queue Codel 3 * 4 * Copyright (C) 2012,2015 Eric Dumazet <edumazet@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 48#include "utils.h" 49#include "tc_util.h" 50 51static void explain(void) 52{ 53 fprintf(stderr, "Usage: ... fq_codel [ limit PACKETS ] [ flows NUMBER ]\n"); 54 fprintf(stderr, " [ target TIME] [ interval TIME ]\n"); 55 fprintf(stderr, " [ quantum BYTES ] [ [no]ecn ]\n"); 56 fprintf(stderr, " [ ce_threshold TIME ]\n"); 57} 58 59static int fq_codel_parse_opt(struct qdisc_util *qu, int argc, char **argv, 60 struct nlmsghdr *n) 61{ 62 unsigned limit = 0; 63 unsigned flows = 0; 64 unsigned target = 0; 65 unsigned interval = 0; 66 unsigned quantum = 0; 67 unsigned ce_threshold = ~0U; 68 int ecn = -1; 69 struct rtattr *tail; 70 71 while (argc > 0) { 72 if (strcmp(*argv, "limit") == 0) { 73 NEXT_ARG(); 74 if (get_unsigned(&limit, *argv, 0)) { 75 fprintf(stderr, "Illegal \"limit\"\n"); 76 return -1; 77 } 78 } else if (strcmp(*argv, "flows") == 0) { 79 NEXT_ARG(); 80 if (get_unsigned(&flows, *argv, 0)) { 81 fprintf(stderr, "Illegal \"flows\"\n"); 82 return -1; 83 } 84 } else if (strcmp(*argv, "quantum") == 0) { 85 NEXT_ARG(); 86 if (get_unsigned(&quantum, *argv, 0)) { 87 fprintf(stderr, "Illegal \"quantum\"\n"); 88 return -1; 89 } 90 } else if (strcmp(*argv, "target") == 0) { 91 NEXT_ARG(); 92 if (get_time(&target, *argv)) { 93 fprintf(stderr, "Illegal \"target\"\n"); 94 return -1; 95 } 96 } else if (strcmp(*argv, "ce_threshold") == 0) { 97 NEXT_ARG(); 98 if (get_time(&ce_threshold, *argv)) { 99 fprintf(stderr, "Illegal \"ce_threshold\"\n"); 100 return -1; 101 } 102 } else if (strcmp(*argv, "interval") == 0) { 103 NEXT_ARG(); 104 if (get_time(&interval, *argv)) { 105 fprintf(stderr, "Illegal \"interval\"\n"); 106 return -1; 107 } 108 } else if (strcmp(*argv, "ecn") == 0) { 109 ecn = 1; 110 } else if (strcmp(*argv, "noecn") == 0) { 111 ecn = 0; 112 } else if (strcmp(*argv, "help") == 0) { 113 explain(); 114 return -1; 115 } else { 116 fprintf(stderr, "What is \"%s\"?\n", *argv); 117 explain(); 118 return -1; 119 } 120 argc--; argv++; 121 } 122 123 tail = NLMSG_TAIL(n); 124 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0); 125 if (limit) 126 addattr_l(n, 1024, TCA_FQ_CODEL_LIMIT, &limit, sizeof(limit)); 127 if (flows) 128 addattr_l(n, 1024, TCA_FQ_CODEL_FLOWS, &flows, sizeof(flows)); 129 if (quantum) 130 addattr_l(n, 1024, TCA_FQ_CODEL_QUANTUM, &quantum, sizeof(quantum)); 131 if (interval) 132 addattr_l(n, 1024, TCA_FQ_CODEL_INTERVAL, &interval, sizeof(interval)); 133 if (target) 134 addattr_l(n, 1024, TCA_FQ_CODEL_TARGET, &target, sizeof(target)); 135 if (ecn != -1) 136 addattr_l(n, 1024, TCA_FQ_CODEL_ECN, &ecn, sizeof(ecn)); 137 if (ce_threshold != ~0U) 138 addattr_l(n, 1024, TCA_FQ_CODEL_CE_THRESHOLD, 139 &ce_threshold, sizeof(ce_threshold)); 140 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; 141 return 0; 142} 143 144static int fq_codel_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) 145{ 146 struct rtattr *tb[TCA_FQ_CODEL_MAX + 1]; 147 unsigned limit; 148 unsigned flows; 149 unsigned interval; 150 unsigned target; 151 unsigned ecn; 152 unsigned quantum; 153 unsigned ce_threshold; 154 SPRINT_BUF(b1); 155 156 if (opt == NULL) 157 return 0; 158 159 parse_rtattr_nested(tb, TCA_FQ_CODEL_MAX, opt); 160 161 if (tb[TCA_FQ_CODEL_LIMIT] && 162 RTA_PAYLOAD(tb[TCA_FQ_CODEL_LIMIT]) >= sizeof(__u32)) { 163 limit = rta_getattr_u32(tb[TCA_FQ_CODEL_LIMIT]); 164 fprintf(f, "limit %up ", limit); 165 } 166 if (tb[TCA_FQ_CODEL_FLOWS] && 167 RTA_PAYLOAD(tb[TCA_FQ_CODEL_FLOWS]) >= sizeof(__u32)) { 168 flows = rta_getattr_u32(tb[TCA_FQ_CODEL_FLOWS]); 169 fprintf(f, "flows %u ", flows); 170 } 171 if (tb[TCA_FQ_CODEL_QUANTUM] && 172 RTA_PAYLOAD(tb[TCA_FQ_CODEL_QUANTUM]) >= sizeof(__u32)) { 173 quantum = rta_getattr_u32(tb[TCA_FQ_CODEL_QUANTUM]); 174 fprintf(f, "quantum %u ", quantum); 175 } 176 if (tb[TCA_FQ_CODEL_TARGET] && 177 RTA_PAYLOAD(tb[TCA_FQ_CODEL_TARGET]) >= sizeof(__u32)) { 178 target = rta_getattr_u32(tb[TCA_FQ_CODEL_TARGET]); 179 fprintf(f, "target %s ", sprint_time(target, b1)); 180 } 181 if (tb[TCA_FQ_CODEL_CE_THRESHOLD] && 182 RTA_PAYLOAD(tb[TCA_FQ_CODEL_CE_THRESHOLD]) >= sizeof(__u32)) { 183 ce_threshold = rta_getattr_u32(tb[TCA_FQ_CODEL_CE_THRESHOLD]); 184 fprintf(f, "ce_threshold %s ", sprint_time(ce_threshold, b1)); 185 } 186 if (tb[TCA_FQ_CODEL_INTERVAL] && 187 RTA_PAYLOAD(tb[TCA_FQ_CODEL_INTERVAL]) >= sizeof(__u32)) { 188 interval = rta_getattr_u32(tb[TCA_FQ_CODEL_INTERVAL]); 189 fprintf(f, "interval %s ", sprint_time(interval, b1)); 190 } 191 if (tb[TCA_FQ_CODEL_ECN] && 192 RTA_PAYLOAD(tb[TCA_FQ_CODEL_ECN]) >= sizeof(__u32)) { 193 ecn = rta_getattr_u32(tb[TCA_FQ_CODEL_ECN]); 194 if (ecn) 195 fprintf(f, "ecn "); 196 } 197 198 return 0; 199} 200 201static int fq_codel_print_xstats(struct qdisc_util *qu, FILE *f, 202 struct rtattr *xstats) 203{ 204 struct tc_fq_codel_xstats _st, *st; 205 SPRINT_BUF(b1); 206 207 if (xstats == NULL) 208 return 0; 209 210 st = RTA_DATA(xstats); 211 if (RTA_PAYLOAD(xstats) < sizeof(*st)) { 212 memset(&_st, 0, sizeof(_st)); 213 memcpy(&_st, st, RTA_PAYLOAD(xstats)); 214 st = &_st; 215 } 216 if (st->type == TCA_FQ_CODEL_XSTATS_QDISC) { 217 fprintf(f, " maxpacket %u drop_overlimit %u new_flow_count %u ecn_mark %u", 218 st->qdisc_stats.maxpacket, 219 st->qdisc_stats.drop_overlimit, 220 st->qdisc_stats.new_flow_count, 221 st->qdisc_stats.ecn_mark); 222 if (st->qdisc_stats.ce_mark) 223 fprintf(f, " ce_mark %u", st->qdisc_stats.ce_mark); 224 fprintf(f, "\n new_flows_len %u old_flows_len %u", 225 st->qdisc_stats.new_flows_len, 226 st->qdisc_stats.old_flows_len); 227 } 228 if (st->type == TCA_FQ_CODEL_XSTATS_CLASS) { 229 fprintf(f, " deficit %d count %u lastcount %u ldelay %s", 230 st->class_stats.deficit, 231 st->class_stats.count, 232 st->class_stats.lastcount, 233 sprint_time(st->class_stats.ldelay, b1)); 234 if (st->class_stats.dropping) { 235 fprintf(f, " dropping"); 236 if (st->class_stats.drop_next < 0) 237 fprintf(f, " drop_next -%s", 238 sprint_time(-st->class_stats.drop_next, b1)); 239 else 240 fprintf(f, " drop_next %s", 241 sprint_time(st->class_stats.drop_next, b1)); 242 } 243 } 244 return 0; 245 246} 247 248struct qdisc_util fq_codel_qdisc_util = { 249 .id = "fq_codel", 250 .parse_qopt = fq_codel_parse_opt, 251 .print_qopt = fq_codel_print_opt, 252 .print_xstats = fq_codel_print_xstats, 253}; 254