osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * q_cbq.c CBQ. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <unistd.h> |
| 16 | #include <syslog.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <sys/socket.h> |
| 19 | #include <netinet/in.h> |
| 20 | #include <arpa/inet.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include "utils.h" |
| 24 | #include "tc_util.h" |
| 25 | #include "tc_cbq.h" |
| 26 | |
| 27 | static void explain_class(void) |
| 28 | { |
| 29 | fprintf(stderr, "Usage: ... cbq bandwidth BPS rate BPS maxburst PKTS [ avpkt BYTES ]\n"); |
| 30 | fprintf(stderr, " [ minburst PKTS ] [ bounded ] [ isolated ]\n"); |
| 31 | fprintf(stderr, " [ allot BYTES ] [ mpu BYTES ] [ weight RATE ]\n"); |
| 32 | fprintf(stderr, " [ prio NUMBER ] [ cell BYTES ] [ ewma LOG ]\n"); |
| 33 | fprintf(stderr, " [ estimator INTERVAL TIME_CONSTANT ]\n"); |
| 34 | fprintf(stderr, " [ split CLASSID ] [ defmap MASK/CHANGE ]\n"); |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 35 | fprintf(stderr, " [ overhead BYTES ] [ linklayer TYPE ]\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | static void explain(void) |
| 39 | { |
| 40 | fprintf(stderr, "Usage: ... cbq bandwidth BPS avpkt BYTES [ mpu BYTES ]\n"); |
| 41 | fprintf(stderr, " [ cell BYTES ] [ ewma LOG ]\n"); |
| 42 | } |
| 43 | |
| 44 | static void explain1(char *arg) |
| 45 | { |
| 46 | fprintf(stderr, "Illegal \"%s\"\n", arg); |
| 47 | } |
| 48 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 49 | |
| 50 | static int cbq_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n) |
| 51 | { |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 52 | struct tc_ratespec r = {}; |
| 53 | struct tc_cbq_lssopt lss = {}; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 54 | __u32 rtab[256]; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 55 | unsigned mpu = 0, avpkt = 0, allot = 0; |
| 56 | unsigned short overhead = 0; |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 57 | unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */ |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 58 | int cell_log = -1; |
| 59 | int ewma_log = -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 60 | struct rtattr *tail; |
| 61 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 62 | while (argc > 0) { |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 63 | if (matches(*argv, "bandwidth") == 0 || |
| 64 | matches(*argv, "rate") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 65 | NEXT_ARG(); |
| 66 | if (get_rate(&r.rate, *argv)) { |
| 67 | explain1("bandwidth"); |
| 68 | return -1; |
| 69 | } |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 70 | } else if (matches(*argv, "ewma") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 71 | NEXT_ARG(); |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 72 | if (get_integer(&ewma_log, *argv, 0)) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 73 | explain1("ewma"); |
| 74 | return -1; |
| 75 | } |
| 76 | if (ewma_log > 31) { |
| 77 | fprintf(stderr, "ewma_log must be < 32\n"); |
| 78 | return -1; |
| 79 | } |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 80 | } else if (matches(*argv, "cell") == 0) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 81 | unsigned int cell; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 82 | int i; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 83 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 84 | NEXT_ARG(); |
| 85 | if (get_size(&cell, *argv)) { |
| 86 | explain1("cell"); |
| 87 | return -1; |
| 88 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 89 | for (i = 0; i < 32; i++) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 90 | if ((1<<i) == cell) |
| 91 | break; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 92 | if (i >= 32) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 93 | fprintf(stderr, "cell must be 2^n\n"); |
| 94 | return -1; |
| 95 | } |
| 96 | cell_log = i; |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 97 | } else if (matches(*argv, "avpkt") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 98 | NEXT_ARG(); |
| 99 | if (get_size(&avpkt, *argv)) { |
| 100 | explain1("avpkt"); |
| 101 | return -1; |
| 102 | } |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 103 | } else if (matches(*argv, "mpu") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 104 | NEXT_ARG(); |
| 105 | if (get_size(&mpu, *argv)) { |
| 106 | explain1("mpu"); |
| 107 | return -1; |
| 108 | } |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 109 | } else if (matches(*argv, "allot") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 110 | NEXT_ARG(); |
| 111 | /* Accept and ignore "allot" for backward compatibility */ |
| 112 | if (get_size(&allot, *argv)) { |
| 113 | explain1("allot"); |
| 114 | return -1; |
| 115 | } |
Jesper Dangaard Brouer | 08fd018 | 2008-03-23 23:50:49 +0100 | [diff] [blame] | 116 | } else if (matches(*argv, "overhead") == 0) { |
| 117 | NEXT_ARG(); |
| 118 | if (get_u16(&overhead, *argv, 10)) { |
| 119 | explain1("overhead"); return -1; |
| 120 | } |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 121 | } else if (matches(*argv, "linklayer") == 0) { |
| 122 | NEXT_ARG(); |
| 123 | if (get_linklayer(&linklayer, *argv)) { |
| 124 | explain1("linklayer"); return -1; |
| 125 | } |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 126 | } else if (matches(*argv, "help") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 127 | explain(); |
| 128 | return -1; |
| 129 | } else { |
| 130 | fprintf(stderr, "What is \"%s\"?\n", *argv); |
| 131 | explain(); |
| 132 | return -1; |
| 133 | } |
| 134 | argc--; argv++; |
| 135 | } |
| 136 | |
| 137 | /* OK. All options are parsed. */ |
| 138 | |
| 139 | if (r.rate == 0) { |
| 140 | fprintf(stderr, "CBQ: bandwidth is required parameter.\n"); |
| 141 | return -1; |
| 142 | } |
| 143 | if (avpkt == 0) { |
| 144 | fprintf(stderr, "CBQ: \"avpkt\" is required.\n"); |
| 145 | return -1; |
| 146 | } |
| 147 | if (allot < (avpkt*3)/2) |
| 148 | allot = (avpkt*3)/2; |
| 149 | |
Jesper Dangaard Brouer | d5f46f9 | 2007-09-05 10:47:47 +0200 | [diff] [blame] | 150 | r.mpu = mpu; |
Jesper Dangaard Brouer | 08fd018 | 2008-03-23 23:50:49 +0100 | [diff] [blame] | 151 | r.overhead = overhead; |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 152 | if (tc_calc_rtable(&r, rtab, cell_log, allot, linklayer) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 153 | fprintf(stderr, "CBQ: failed to calculate rate table.\n"); |
| 154 | return -1; |
| 155 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 156 | |
| 157 | if (ewma_log < 0) |
| 158 | ewma_log = TC_CBQ_DEF_EWMA; |
| 159 | lss.ewma_log = ewma_log; |
Patrick McHardy | 476daa7 | 2007-03-04 20:14:56 +0100 | [diff] [blame] | 160 | lss.maxidle = tc_calc_xmittime(r.rate, avpkt); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 161 | lss.change = TCF_CBQ_LSS_MAXIDLE|TCF_CBQ_LSS_EWMA|TCF_CBQ_LSS_AVPKT; |
| 162 | lss.avpkt = avpkt; |
| 163 | |
5!tgraf | 034102f | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 164 | tail = NLMSG_TAIL(n); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 165 | addattr_l(n, 1024, TCA_OPTIONS, NULL, 0); |
| 166 | addattr_l(n, 1024, TCA_CBQ_RATE, &r, sizeof(r)); |
| 167 | addattr_l(n, 1024, TCA_CBQ_LSSOPT, &lss, sizeof(lss)); |
| 168 | addattr_l(n, 3024, TCA_CBQ_RTAB, rtab, 1024); |
| 169 | if (show_raw) { |
| 170 | int i; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 171 | |
| 172 | for (i = 0; i < 256; i++) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 173 | printf("%u ", rtab[i]); |
| 174 | printf("\n"); |
| 175 | } |
5!tgraf | 034102f | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 176 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int cbq_parse_class_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n) |
| 181 | { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 182 | int wrr_ok = 0, fopt_ok = 0; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 183 | struct tc_ratespec r = {}; |
| 184 | struct tc_cbq_lssopt lss = {}; |
| 185 | struct tc_cbq_wrropt wrr = {}; |
| 186 | struct tc_cbq_fopt fopt = {}; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 187 | __u32 rtab[256]; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 188 | unsigned mpu = 0; |
| 189 | int cell_log = -1; |
| 190 | int ewma_log = -1; |
| 191 | unsigned int bndw = 0; |
| 192 | unsigned minburst = 0, maxburst = 0; |
| 193 | unsigned short overhead = 0; |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 194 | unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */ |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 195 | struct rtattr *tail; |
| 196 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 197 | while (argc > 0) { |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 198 | if (matches(*argv, "rate") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 199 | NEXT_ARG(); |
| 200 | if (get_rate(&r.rate, *argv)) { |
| 201 | explain1("rate"); |
| 202 | return -1; |
| 203 | } |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 204 | } else if (matches(*argv, "bandwidth") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 205 | NEXT_ARG(); |
| 206 | if (get_rate(&bndw, *argv)) { |
| 207 | explain1("bandwidth"); |
| 208 | return -1; |
| 209 | } |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 210 | } else if (matches(*argv, "minidle") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 211 | NEXT_ARG(); |
| 212 | if (get_u32(&lss.minidle, *argv, 0)) { |
| 213 | explain1("minidle"); |
| 214 | return -1; |
| 215 | } |
| 216 | lss.change |= TCF_CBQ_LSS_MINIDLE; |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 217 | } else if (matches(*argv, "minburst") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 218 | NEXT_ARG(); |
| 219 | if (get_u32(&minburst, *argv, 0)) { |
| 220 | explain1("minburst"); |
| 221 | return -1; |
| 222 | } |
| 223 | lss.change |= TCF_CBQ_LSS_OFFTIME; |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 224 | } else if (matches(*argv, "maxburst") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 225 | NEXT_ARG(); |
| 226 | if (get_u32(&maxburst, *argv, 0)) { |
| 227 | explain1("maxburst"); |
| 228 | return -1; |
| 229 | } |
| 230 | lss.change |= TCF_CBQ_LSS_MAXIDLE; |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 231 | } else if (matches(*argv, "bounded") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 232 | lss.flags |= TCF_CBQ_LSS_BOUNDED; |
| 233 | lss.change |= TCF_CBQ_LSS_FLAGS; |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 234 | } else if (matches(*argv, "borrow") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 235 | lss.flags &= ~TCF_CBQ_LSS_BOUNDED; |
| 236 | lss.change |= TCF_CBQ_LSS_FLAGS; |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 237 | } else if (matches(*argv, "isolated") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 238 | lss.flags |= TCF_CBQ_LSS_ISOLATED; |
| 239 | lss.change |= TCF_CBQ_LSS_FLAGS; |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 240 | } else if (matches(*argv, "sharing") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 241 | lss.flags &= ~TCF_CBQ_LSS_ISOLATED; |
| 242 | lss.change |= TCF_CBQ_LSS_FLAGS; |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 243 | } else if (matches(*argv, "ewma") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 244 | NEXT_ARG(); |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 245 | if (get_integer(&ewma_log, *argv, 0)) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 246 | explain1("ewma"); |
| 247 | return -1; |
| 248 | } |
| 249 | if (ewma_log > 31) { |
| 250 | fprintf(stderr, "ewma_log must be < 32\n"); |
| 251 | return -1; |
| 252 | } |
| 253 | lss.change |= TCF_CBQ_LSS_EWMA; |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 254 | } else if (matches(*argv, "cell") == 0) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 255 | unsigned int cell; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 256 | int i; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 257 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 258 | NEXT_ARG(); |
| 259 | if (get_size(&cell, *argv)) { |
| 260 | explain1("cell"); |
| 261 | return -1; |
| 262 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 263 | for (i = 0; i < 32; i++) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 264 | if ((1<<i) == cell) |
| 265 | break; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 266 | if (i >= 32) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 267 | fprintf(stderr, "cell must be 2^n\n"); |
| 268 | return -1; |
| 269 | } |
| 270 | cell_log = i; |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 271 | } else if (matches(*argv, "prio") == 0) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 272 | unsigned int prio; |
| 273 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 274 | NEXT_ARG(); |
| 275 | if (get_u32(&prio, *argv, 0)) { |
| 276 | explain1("prio"); |
| 277 | return -1; |
| 278 | } |
| 279 | if (prio > TC_CBQ_MAXPRIO) { |
| 280 | fprintf(stderr, "\"prio\" must be number in the range 1...%d\n", TC_CBQ_MAXPRIO); |
| 281 | return -1; |
| 282 | } |
| 283 | wrr.priority = prio; |
| 284 | wrr_ok++; |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 285 | } else if (matches(*argv, "allot") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 286 | NEXT_ARG(); |
| 287 | if (get_size(&wrr.allot, *argv)) { |
| 288 | explain1("allot"); |
| 289 | return -1; |
| 290 | } |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 291 | } else if (matches(*argv, "avpkt") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 292 | NEXT_ARG(); |
| 293 | if (get_size(&lss.avpkt, *argv)) { |
| 294 | explain1("avpkt"); |
| 295 | return -1; |
| 296 | } |
| 297 | lss.change |= TCF_CBQ_LSS_AVPKT; |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 298 | } else if (matches(*argv, "mpu") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 299 | NEXT_ARG(); |
| 300 | if (get_size(&mpu, *argv)) { |
| 301 | explain1("mpu"); |
| 302 | return -1; |
| 303 | } |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 304 | } else if (matches(*argv, "weight") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 305 | NEXT_ARG(); |
| 306 | if (get_size(&wrr.weight, *argv)) { |
| 307 | explain1("weight"); |
| 308 | return -1; |
| 309 | } |
| 310 | wrr_ok++; |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 311 | } else if (matches(*argv, "split") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 312 | NEXT_ARG(); |
| 313 | if (get_tc_classid(&fopt.split, *argv)) { |
| 314 | fprintf(stderr, "Invalid split node ID.\n"); |
Florian Westphal | 8d8de11 | 2010-01-12 21:45:01 +0100 | [diff] [blame] | 315 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 316 | } |
| 317 | fopt_ok++; |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 318 | } else if (matches(*argv, "defmap") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 319 | int err; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 320 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 321 | NEXT_ARG(); |
| 322 | err = sscanf(*argv, "%08x/%08x", &fopt.defmap, &fopt.defchange); |
| 323 | if (err < 1) { |
| 324 | fprintf(stderr, "Invalid defmap, should be MASK32[/MASK]\n"); |
| 325 | return -1; |
| 326 | } |
| 327 | if (err == 1) |
| 328 | fopt.defchange = ~0; |
| 329 | fopt_ok++; |
Jesper Dangaard Brouer | 08fd018 | 2008-03-23 23:50:49 +0100 | [diff] [blame] | 330 | } else if (matches(*argv, "overhead") == 0) { |
| 331 | NEXT_ARG(); |
| 332 | if (get_u16(&overhead, *argv, 10)) { |
| 333 | explain1("overhead"); return -1; |
| 334 | } |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 335 | } else if (matches(*argv, "linklayer") == 0) { |
| 336 | NEXT_ARG(); |
| 337 | if (get_linklayer(&linklayer, *argv)) { |
| 338 | explain1("linklayer"); return -1; |
| 339 | } |
Jesper Dangaard Brouer | 1db5e2e | 2008-03-23 23:49:11 +0100 | [diff] [blame] | 340 | } else if (matches(*argv, "help") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 341 | explain_class(); |
| 342 | return -1; |
| 343 | } else { |
| 344 | fprintf(stderr, "What is \"%s\"?\n", *argv); |
| 345 | explain_class(); |
| 346 | return -1; |
| 347 | } |
| 348 | argc--; argv++; |
| 349 | } |
| 350 | |
| 351 | /* OK. All options are parsed. */ |
| 352 | |
| 353 | /* 1. Prepare link sharing scheduler parameters */ |
| 354 | if (r.rate) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 355 | unsigned int pktsize = wrr.allot; |
| 356 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 357 | if (wrr.allot < (lss.avpkt*3)/2) |
| 358 | wrr.allot = (lss.avpkt*3)/2; |
Jesper Dangaard Brouer | d5f46f9 | 2007-09-05 10:47:47 +0200 | [diff] [blame] | 359 | r.mpu = mpu; |
Jesper Dangaard Brouer | 08fd018 | 2008-03-23 23:50:49 +0100 | [diff] [blame] | 360 | r.overhead = overhead; |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 361 | if (tc_calc_rtable(&r, rtab, cell_log, pktsize, linklayer) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 362 | fprintf(stderr, "CBQ: failed to calculate rate table.\n"); |
| 363 | return -1; |
| 364 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 365 | } |
| 366 | if (ewma_log < 0) |
| 367 | ewma_log = TC_CBQ_DEF_EWMA; |
| 368 | lss.ewma_log = ewma_log; |
| 369 | if (lss.change&(TCF_CBQ_LSS_OFFTIME|TCF_CBQ_LSS_MAXIDLE)) { |
| 370 | if (lss.avpkt == 0) { |
| 371 | fprintf(stderr, "CBQ: avpkt is required for max/minburst.\n"); |
| 372 | return -1; |
| 373 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 374 | if (bndw == 0 || r.rate == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 375 | fprintf(stderr, "CBQ: bandwidth&rate are required for max/minburst.\n"); |
| 376 | return -1; |
| 377 | } |
| 378 | } |
| 379 | if (wrr.priority == 0 && (n->nlmsg_flags&NLM_F_EXCL)) { |
| 380 | wrr_ok = 1; |
| 381 | wrr.priority = TC_CBQ_MAXPRIO; |
| 382 | if (wrr.allot == 0) |
| 383 | wrr.allot = (lss.avpkt*3)/2; |
| 384 | } |
| 385 | if (wrr_ok) { |
| 386 | if (wrr.weight == 0) |
| 387 | wrr.weight = (wrr.priority == TC_CBQ_MAXPRIO) ? 1 : r.rate; |
| 388 | if (wrr.allot == 0) { |
| 389 | fprintf(stderr, "CBQ: \"allot\" is required to set WRR parameters.\n"); |
| 390 | return -1; |
| 391 | } |
| 392 | } |
| 393 | if (lss.change&TCF_CBQ_LSS_MAXIDLE) { |
| 394 | lss.maxidle = tc_cbq_calc_maxidle(bndw, r.rate, lss.avpkt, ewma_log, maxburst); |
| 395 | lss.change |= TCF_CBQ_LSS_MAXIDLE; |
| 396 | lss.change |= TCF_CBQ_LSS_EWMA|TCF_CBQ_LSS_AVPKT; |
| 397 | } |
| 398 | if (lss.change&TCF_CBQ_LSS_OFFTIME) { |
| 399 | lss.offtime = tc_cbq_calc_offtime(bndw, r.rate, lss.avpkt, ewma_log, minburst); |
| 400 | lss.change |= TCF_CBQ_LSS_OFFTIME; |
| 401 | lss.change |= TCF_CBQ_LSS_EWMA|TCF_CBQ_LSS_AVPKT; |
| 402 | } |
| 403 | if (lss.change&TCF_CBQ_LSS_MINIDLE) { |
| 404 | lss.minidle <<= lss.ewma_log; |
| 405 | lss.change |= TCF_CBQ_LSS_EWMA; |
| 406 | } |
| 407 | |
5!tgraf | 034102f | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 408 | tail = NLMSG_TAIL(n); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 409 | addattr_l(n, 1024, TCA_OPTIONS, NULL, 0); |
| 410 | if (lss.change) { |
| 411 | lss.change |= TCF_CBQ_LSS_FLAGS; |
| 412 | addattr_l(n, 1024, TCA_CBQ_LSSOPT, &lss, sizeof(lss)); |
| 413 | } |
| 414 | if (wrr_ok) |
| 415 | addattr_l(n, 1024, TCA_CBQ_WRROPT, &wrr, sizeof(wrr)); |
| 416 | if (fopt_ok) |
| 417 | addattr_l(n, 1024, TCA_CBQ_FOPT, &fopt, sizeof(fopt)); |
| 418 | if (r.rate) { |
| 419 | addattr_l(n, 1024, TCA_CBQ_RATE, &r, sizeof(r)); |
| 420 | addattr_l(n, 3024, TCA_CBQ_RTAB, rtab, 1024); |
| 421 | if (show_raw) { |
| 422 | int i; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 423 | |
| 424 | for (i = 0; i < 256; i++) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 425 | printf("%u ", rtab[i]); |
| 426 | printf("\n"); |
| 427 | } |
| 428 | } |
5!tgraf | 034102f | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 429 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | |
| 434 | static int cbq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) |
| 435 | { |
| 436 | struct rtattr *tb[TCA_CBQ_MAX+1]; |
| 437 | struct tc_ratespec *r = NULL; |
| 438 | struct tc_cbq_lssopt *lss = NULL; |
| 439 | struct tc_cbq_wrropt *wrr = NULL; |
| 440 | struct tc_cbq_fopt *fopt = NULL; |
| 441 | struct tc_cbq_ovl *ovl = NULL; |
Jesper Dangaard Brouer | 3e92ff5 | 2013-08-30 14:02:10 +0200 | [diff] [blame] | 442 | unsigned int linklayer; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 443 | |
Patrick McHardy | bd29e35 | 2007-03-04 20:15:01 +0100 | [diff] [blame] | 444 | SPRINT_BUF(b1); |
Jesper Dangaard Brouer | 3e92ff5 | 2013-08-30 14:02:10 +0200 | [diff] [blame] | 445 | SPRINT_BUF(b2); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 446 | |
| 447 | if (opt == NULL) |
| 448 | return 0; |
| 449 | |
5!tgraf | 021ed13 | 2005-01-18 22:11:58 +0000 | [diff] [blame] | 450 | parse_rtattr_nested(tb, TCA_CBQ_MAX, opt); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 451 | |
| 452 | if (tb[TCA_CBQ_RATE]) { |
| 453 | if (RTA_PAYLOAD(tb[TCA_CBQ_RATE]) < sizeof(*r)) |
| 454 | fprintf(stderr, "CBQ: too short rate opt\n"); |
| 455 | else |
| 456 | r = RTA_DATA(tb[TCA_CBQ_RATE]); |
| 457 | } |
| 458 | if (tb[TCA_CBQ_LSSOPT]) { |
| 459 | if (RTA_PAYLOAD(tb[TCA_CBQ_LSSOPT]) < sizeof(*lss)) |
| 460 | fprintf(stderr, "CBQ: too short lss opt\n"); |
| 461 | else |
| 462 | lss = RTA_DATA(tb[TCA_CBQ_LSSOPT]); |
| 463 | } |
| 464 | if (tb[TCA_CBQ_WRROPT]) { |
| 465 | if (RTA_PAYLOAD(tb[TCA_CBQ_WRROPT]) < sizeof(*wrr)) |
| 466 | fprintf(stderr, "CBQ: too short wrr opt\n"); |
| 467 | else |
| 468 | wrr = RTA_DATA(tb[TCA_CBQ_WRROPT]); |
| 469 | } |
| 470 | if (tb[TCA_CBQ_FOPT]) { |
| 471 | if (RTA_PAYLOAD(tb[TCA_CBQ_FOPT]) < sizeof(*fopt)) |
| 472 | fprintf(stderr, "CBQ: too short fopt\n"); |
| 473 | else |
| 474 | fopt = RTA_DATA(tb[TCA_CBQ_FOPT]); |
| 475 | } |
| 476 | if (tb[TCA_CBQ_OVL_STRATEGY]) { |
| 477 | if (RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]) < sizeof(*ovl)) |
| 478 | fprintf(stderr, "CBQ: too short overlimit strategy %u/%u\n", |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 479 | (unsigned int) RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]), |
| 480 | (unsigned int) sizeof(*ovl)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 481 | else |
| 482 | ovl = RTA_DATA(tb[TCA_CBQ_OVL_STRATEGY]); |
| 483 | } |
| 484 | |
| 485 | if (r) { |
| 486 | char buf[64]; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 487 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 488 | print_rate(buf, sizeof(buf), r->rate); |
| 489 | fprintf(f, "rate %s ", buf); |
Jesper Dangaard Brouer | 3e92ff5 | 2013-08-30 14:02:10 +0200 | [diff] [blame] | 490 | linklayer = (r->linklayer & TC_LINKLAYER_MASK); |
| 491 | if (linklayer > TC_LINKLAYER_ETHERNET || show_details) |
| 492 | fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b2)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 493 | if (show_details) { |
| 494 | fprintf(f, "cell %ub ", 1<<r->cell_log); |
| 495 | if (r->mpu) |
| 496 | fprintf(f, "mpu %ub ", r->mpu); |
Jesper Dangaard Brouer | 08fd018 | 2008-03-23 23:50:49 +0100 | [diff] [blame] | 497 | if (r->overhead) |
| 498 | fprintf(f, "overhead %ub ", r->overhead); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | if (lss && lss->flags) { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 502 | int comma = 0; |
| 503 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 504 | fprintf(f, "("); |
| 505 | if (lss->flags&TCF_CBQ_LSS_BOUNDED) { |
| 506 | fprintf(f, "bounded"); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 507 | comma = 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 508 | } |
| 509 | if (lss->flags&TCF_CBQ_LSS_ISOLATED) { |
| 510 | if (comma) |
| 511 | fprintf(f, ","); |
| 512 | fprintf(f, "isolated"); |
| 513 | } |
| 514 | fprintf(f, ") "); |
| 515 | } |
| 516 | if (wrr) { |
| 517 | if (wrr->priority != TC_CBQ_MAXPRIO) |
| 518 | fprintf(f, "prio %u", wrr->priority); |
| 519 | else |
| 520 | fprintf(f, "prio no-transmit"); |
| 521 | if (show_details) { |
| 522 | char buf[64]; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 523 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 524 | fprintf(f, "/%u ", wrr->cpriority); |
| 525 | if (wrr->weight != 1) { |
| 526 | print_rate(buf, sizeof(buf), wrr->weight); |
| 527 | fprintf(f, "weight %s ", buf); |
| 528 | } |
| 529 | if (wrr->allot) |
| 530 | fprintf(f, "allot %ub ", wrr->allot); |
| 531 | } |
| 532 | } |
| 533 | if (lss && show_details) { |
| 534 | fprintf(f, "\nlevel %u ewma %u avpkt %ub ", lss->level, lss->ewma_log, lss->avpkt); |
| 535 | if (lss->maxidle) { |
Patrick McHardy | bd29e35 | 2007-03-04 20:15:01 +0100 | [diff] [blame] | 536 | fprintf(f, "maxidle %s ", sprint_ticks(lss->maxidle>>lss->ewma_log, b1)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 537 | if (show_raw) |
| 538 | fprintf(f, "[%08x] ", lss->maxidle); |
| 539 | } |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 540 | if (lss->minidle != 0x7fffffff) { |
Patrick McHardy | bd29e35 | 2007-03-04 20:15:01 +0100 | [diff] [blame] | 541 | fprintf(f, "minidle %s ", sprint_ticks(lss->minidle>>lss->ewma_log, b1)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 542 | if (show_raw) |
| 543 | fprintf(f, "[%08x] ", lss->minidle); |
| 544 | } |
| 545 | if (lss->offtime) { |
Patrick McHardy | bd29e35 | 2007-03-04 20:15:01 +0100 | [diff] [blame] | 546 | fprintf(f, "offtime %s ", sprint_ticks(lss->offtime, b1)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 547 | if (show_raw) |
| 548 | fprintf(f, "[%08x] ", lss->offtime); |
| 549 | } |
| 550 | } |
| 551 | if (fopt && show_details) { |
| 552 | char buf[64]; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 553 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 554 | print_tc_classid(buf, sizeof(buf), fopt->split); |
| 555 | fprintf(f, "\nsplit %s ", buf); |
| 556 | if (fopt->defmap) { |
| 557 | fprintf(f, "defmap %08x", fopt->defmap); |
| 558 | } |
| 559 | } |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static int cbq_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats) |
| 564 | { |
| 565 | struct tc_cbq_xstats *st; |
| 566 | |
| 567 | if (xstats == NULL) |
| 568 | return 0; |
| 569 | |
| 570 | if (RTA_PAYLOAD(xstats) < sizeof(*st)) |
| 571 | return -1; |
| 572 | |
| 573 | st = RTA_DATA(xstats); |
| 574 | fprintf(f, " borrowed %u overactions %u avgidle %g undertime %g", st->borrows, |
| 575 | st->overactions, (double)st->avgidle, (double)st->undertime); |
| 576 | return 0; |
| 577 | } |
| 578 | |
net[shemminger]!kaber | 95812b5 | 2004-09-28 18:35:49 +0000 | [diff] [blame] | 579 | struct qdisc_util cbq_qdisc_util = { |
osdl.net!shemminger | f2f99e2 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 580 | .id = "cbq", |
| 581 | .parse_qopt = cbq_parse_opt, |
| 582 | .print_qopt = cbq_print_opt, |
| 583 | .print_xstats = cbq_print_xstats, |
| 584 | .parse_copt = cbq_parse_class_opt, |
| 585 | .print_copt = cbq_print_opt, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 586 | }; |