osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * q_tbf.c TBF. |
| 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 | |
| 26 | static void explain(void) |
| 27 | { |
| 28 | fprintf(stderr, "Usage: ... tbf limit BYTES burst BYTES[/BYTES] rate KBPS [ mtu BYTES[/BYTES] ]\n"); |
Jesper Dangaard Brouer | 2c42579 | 2008-03-23 23:47:49 +0100 | [diff] [blame] | 29 | fprintf(stderr, " [ peakrate KBPS ] [ latency TIME ] "); |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 30 | fprintf(stderr, "[ overhead BYTES ] [ linklayer TYPE ]\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 33 | static void explain1(const char *arg, const char *val) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 34 | { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 35 | fprintf(stderr, "tbf: illegal value for \"%s\": \"%s\"\n", arg, val); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 39 | static int tbf_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n) |
| 40 | { |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 41 | int ok = 0; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 42 | struct tc_tbf_qopt opt = {}; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 43 | __u32 rtab[256]; |
| 44 | __u32 ptab[256]; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 45 | unsigned buffer = 0, mtu = 0, mpu = 0, latency = 0; |
| 46 | int Rcell_log = -1, Pcell_log = -1; |
| 47 | unsigned short overhead = 0; |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 48 | unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */ |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 49 | struct rtattr *tail; |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 50 | __u64 rate64 = 0, prate64 = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 51 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 52 | while (argc > 0) { |
| 53 | if (matches(*argv, "limit") == 0) { |
| 54 | NEXT_ARG(); |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 55 | if (opt.limit) { |
| 56 | fprintf(stderr, "tbf: duplicate \"limit\" specification\n"); |
| 57 | return -1; |
| 58 | } |
| 59 | if (latency) { |
| 60 | fprintf(stderr, "tbf: specifying both \"latency\" and \"limit\" is not allowed\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 61 | return -1; |
| 62 | } |
| 63 | if (get_size(&opt.limit, *argv)) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 64 | explain1("limit", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 65 | return -1; |
| 66 | } |
| 67 | ok++; |
| 68 | } else if (matches(*argv, "latency") == 0) { |
| 69 | NEXT_ARG(); |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 70 | if (latency) { |
| 71 | fprintf(stderr, "tbf: duplicate \"latency\" specification\n"); |
| 72 | return -1; |
| 73 | } |
| 74 | if (opt.limit) { |
| 75 | fprintf(stderr, "tbf: specifying both \"limit\" and \"/latency\" is not allowed\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 76 | return -1; |
| 77 | } |
Patrick McHardy | 8f34caa | 2007-03-04 20:15:00 +0100 | [diff] [blame] | 78 | if (get_time(&latency, *argv)) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 79 | explain1("latency", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 80 | return -1; |
| 81 | } |
| 82 | ok++; |
| 83 | } else if (matches(*argv, "burst") == 0 || |
| 84 | strcmp(*argv, "buffer") == 0 || |
| 85 | strcmp(*argv, "maxburst") == 0) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 86 | const char *parm_name = *argv; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 87 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 88 | NEXT_ARG(); |
| 89 | if (buffer) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 90 | fprintf(stderr, "tbf: duplicate \"buffer/burst/maxburst\" specification\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 91 | return -1; |
| 92 | } |
| 93 | if (get_size_and_cell(&buffer, &Rcell_log, *argv) < 0) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 94 | explain1(parm_name, *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 95 | return -1; |
| 96 | } |
| 97 | ok++; |
| 98 | } else if (strcmp(*argv, "mtu") == 0 || |
| 99 | strcmp(*argv, "minburst") == 0) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 100 | const char *parm_name = *argv; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 101 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 102 | NEXT_ARG(); |
| 103 | if (mtu) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 104 | fprintf(stderr, "tbf: duplicate \"mtu/minburst\" specification\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 105 | return -1; |
| 106 | } |
| 107 | if (get_size_and_cell(&mtu, &Pcell_log, *argv) < 0) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 108 | explain1(parm_name, *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 109 | return -1; |
| 110 | } |
| 111 | ok++; |
| 112 | } else if (strcmp(*argv, "mpu") == 0) { |
| 113 | NEXT_ARG(); |
| 114 | if (mpu) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 115 | fprintf(stderr, "tbf: duplicate \"mpu\" specification\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 116 | return -1; |
| 117 | } |
| 118 | if (get_size(&mpu, *argv)) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 119 | explain1("mpu", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 120 | return -1; |
| 121 | } |
| 122 | ok++; |
| 123 | } else if (strcmp(*argv, "rate") == 0) { |
| 124 | NEXT_ARG(); |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 125 | if (rate64) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 126 | fprintf(stderr, "tbf: duplicate \"rate\" specification\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 127 | return -1; |
| 128 | } |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 129 | if (get_rate64(&rate64, *argv)) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 130 | explain1("rate", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 131 | return -1; |
| 132 | } |
| 133 | ok++; |
| 134 | } else if (matches(*argv, "peakrate") == 0) { |
| 135 | NEXT_ARG(); |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 136 | if (prate64) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 137 | fprintf(stderr, "tbf: duplicate \"peakrate\" specification\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 138 | return -1; |
| 139 | } |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 140 | if (get_rate64(&prate64, *argv)) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 141 | explain1("peakrate", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 142 | return -1; |
| 143 | } |
| 144 | ok++; |
Jesper Dangaard Brouer | 2c42579 | 2008-03-23 23:47:49 +0100 | [diff] [blame] | 145 | } else if (matches(*argv, "overhead") == 0) { |
| 146 | NEXT_ARG(); |
| 147 | if (overhead) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 148 | fprintf(stderr, "tbf: duplicate \"overhead\" specification\n"); |
Jesper Dangaard Brouer | 2c42579 | 2008-03-23 23:47:49 +0100 | [diff] [blame] | 149 | return -1; |
| 150 | } |
| 151 | if (get_u16(&overhead, *argv, 10)) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 152 | explain1("overhead", *argv); return -1; |
Jesper Dangaard Brouer | 2c42579 | 2008-03-23 23:47:49 +0100 | [diff] [blame] | 153 | } |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 154 | } else if (matches(*argv, "linklayer") == 0) { |
| 155 | NEXT_ARG(); |
| 156 | if (get_linklayer(&linklayer, *argv)) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 157 | explain1("linklayer", *argv); return -1; |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 158 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 159 | } else if (strcmp(*argv, "help") == 0) { |
| 160 | explain(); |
| 161 | return -1; |
| 162 | } else { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 163 | fprintf(stderr, "tbf: unknown parameter \"%s\"\n", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 164 | explain(); |
| 165 | return -1; |
| 166 | } |
| 167 | argc--; argv++; |
| 168 | } |
| 169 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 170 | int verdict = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 171 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 172 | /* Be nice to the user: try to emit all error messages in |
| 173 | * one go rather than reveal one more problem when a |
| 174 | * previous one has been fixed. |
| 175 | */ |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 176 | if (rate64 == 0) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 177 | fprintf(stderr, "tbf: the \"rate\" parameter is mandatory.\n"); |
| 178 | verdict = -1; |
| 179 | } |
| 180 | if (!buffer) { |
| 181 | fprintf(stderr, "tbf: the \"burst\" parameter is mandatory.\n"); |
| 182 | verdict = -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 183 | } |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 184 | if (prate64) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 185 | if (!mtu) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 186 | fprintf(stderr, "tbf: when \"peakrate\" is specified, \"mtu\" must also be specified.\n"); |
| 187 | verdict = -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | if (opt.limit == 0 && latency == 0) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 192 | fprintf(stderr, "tbf: either \"limit\" or \"latency\" is required.\n"); |
| 193 | verdict = -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 196 | if (verdict != 0) { |
| 197 | explain(); |
| 198 | return verdict; |
| 199 | } |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 200 | |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 201 | opt.rate.rate = (rate64 >= (1ULL << 32)) ? ~0U : rate64; |
| 202 | opt.peakrate.rate = (prate64 >= (1ULL << 32)) ? ~0U : prate64; |
| 203 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 204 | if (opt.limit == 0) { |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 205 | double lim = rate64*(double)latency/TIME_UNITS_PER_SEC + buffer; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 206 | |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 207 | if (prate64) { |
| 208 | double lim2 = prate64*(double)latency/TIME_UNITS_PER_SEC + mtu; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 209 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 210 | if (lim2 < lim) |
| 211 | lim = lim2; |
| 212 | } |
| 213 | opt.limit = lim; |
| 214 | } |
| 215 | |
Jesper Dangaard Brouer | 2c42579 | 2008-03-23 23:47:49 +0100 | [diff] [blame] | 216 | opt.rate.mpu = mpu; |
| 217 | opt.rate.overhead = overhead; |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 218 | if (tc_calc_rtable(&opt.rate, rtab, Rcell_log, mtu, linklayer) < 0) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 219 | fprintf(stderr, "tbf: failed to calculate rate table.\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 220 | return -1; |
| 221 | } |
| 222 | opt.buffer = tc_calc_xmittime(opt.rate.rate, buffer); |
Jesper Dangaard Brouer | d5f46f9 | 2007-09-05 10:47:47 +0200 | [diff] [blame] | 223 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 224 | if (opt.peakrate.rate) { |
Jesper Dangaard Brouer | 2c42579 | 2008-03-23 23:47:49 +0100 | [diff] [blame] | 225 | opt.peakrate.mpu = mpu; |
| 226 | opt.peakrate.overhead = overhead; |
Jesper Dangaard Brouer | 292f29b | 2008-04-09 23:01:01 +0200 | [diff] [blame] | 227 | if (tc_calc_rtable(&opt.peakrate, ptab, Pcell_log, mtu, linklayer) < 0) { |
Kees van Reeuwijk | 3bed7bb | 2013-02-19 07:46:04 +0000 | [diff] [blame] | 228 | fprintf(stderr, "tbf: failed to calculate peak rate table.\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 229 | return -1; |
| 230 | } |
| 231 | opt.mtu = tc_calc_xmittime(opt.peakrate.rate, mtu); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 232 | } |
| 233 | |
4!tgraf | 228569c | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 234 | tail = NLMSG_TAIL(n); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 235 | addattr_l(n, 1024, TCA_OPTIONS, NULL, 0); |
| 236 | addattr_l(n, 2024, TCA_TBF_PARMS, &opt, sizeof(opt)); |
Yang Yingliang | a01de0a | 2014-01-16 11:09:13 +0800 | [diff] [blame] | 237 | addattr_l(n, 2124, TCA_TBF_BURST, &buffer, sizeof(buffer)); |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 238 | if (rate64 >= (1ULL << 32)) |
| 239 | addattr_l(n, 2124, TCA_TBF_RATE64, &rate64, sizeof(rate64)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 240 | addattr_l(n, 3024, TCA_TBF_RTAB, rtab, 1024); |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 241 | if (opt.peakrate.rate) { |
| 242 | if (prate64 >= (1ULL << 32)) |
| 243 | addattr_l(n, 3124, TCA_TBF_PRATE64, &prate64, sizeof(prate64)); |
Yang Yingliang | a01de0a | 2014-01-16 11:09:13 +0800 | [diff] [blame] | 244 | addattr_l(n, 3224, TCA_TBF_PBURST, &mtu, sizeof(mtu)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 245 | addattr_l(n, 4096, TCA_TBF_PTAB, ptab, 1024); |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 246 | } |
4!tgraf | 228569c | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 247 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static int tbf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) |
| 252 | { |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 253 | struct rtattr *tb[TCA_TBF_MAX+1]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 254 | struct tc_tbf_qopt *qopt; |
Jesper Dangaard Brouer | 3e92ff5 | 2013-08-30 14:02:10 +0200 | [diff] [blame] | 255 | unsigned int linklayer; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 256 | double buffer, mtu; |
| 257 | double latency; |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 258 | __u64 rate64 = 0, prate64 = 0; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 259 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 260 | SPRINT_BUF(b1); |
| 261 | SPRINT_BUF(b2); |
Jesper Dangaard Brouer | 3e92ff5 | 2013-08-30 14:02:10 +0200 | [diff] [blame] | 262 | SPRINT_BUF(b3); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 263 | |
| 264 | if (opt == NULL) |
| 265 | return 0; |
| 266 | |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 267 | parse_rtattr_nested(tb, TCA_TBF_MAX, opt); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 268 | |
| 269 | if (tb[TCA_TBF_PARMS] == NULL) |
| 270 | return -1; |
| 271 | |
| 272 | qopt = RTA_DATA(tb[TCA_TBF_PARMS]); |
| 273 | if (RTA_PAYLOAD(tb[TCA_TBF_PARMS]) < sizeof(*qopt)) |
| 274 | return -1; |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 275 | rate64 = qopt->rate.rate; |
| 276 | if (tb[TCA_TBF_RATE64] && |
| 277 | RTA_PAYLOAD(tb[TCA_TBF_RATE64]) >= sizeof(rate64)) |
| 278 | rate64 = rta_getattr_u64(tb[TCA_TBF_RATE64]); |
| 279 | fprintf(f, "rate %s ", sprint_rate(rate64, b1)); |
| 280 | buffer = tc_calc_xmitsize(rate64, qopt->buffer); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 281 | if (show_details) { |
| 282 | fprintf(f, "burst %s/%u mpu %s ", sprint_size(buffer, b1), |
| 283 | 1<<qopt->rate.cell_log, sprint_size(qopt->rate.mpu, b2)); |
| 284 | } else { |
| 285 | fprintf(f, "burst %s ", sprint_size(buffer, b1)); |
| 286 | } |
| 287 | if (show_raw) |
| 288 | fprintf(f, "[%08x] ", qopt->buffer); |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 289 | prate64 = qopt->peakrate.rate; |
| 290 | if (tb[TCA_TBF_PRATE64] && |
| 291 | RTA_PAYLOAD(tb[TCA_TBF_PRATE64]) >= sizeof(prate64)) |
| 292 | prate64 = rta_getattr_u64(tb[TCA_TBF_PRATE64]); |
| 293 | if (prate64) { |
| 294 | fprintf(f, "peakrate %s ", sprint_rate(prate64, b1)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 295 | if (qopt->mtu || qopt->peakrate.mpu) { |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 296 | mtu = tc_calc_xmitsize(prate64, qopt->mtu); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 297 | if (show_details) { |
| 298 | fprintf(f, "mtu %s/%u mpu %s ", sprint_size(mtu, b1), |
| 299 | 1<<qopt->peakrate.cell_log, sprint_size(qopt->peakrate.mpu, b2)); |
| 300 | } else { |
| 301 | fprintf(f, "minburst %s ", sprint_size(mtu, b1)); |
| 302 | } |
| 303 | if (show_raw) |
| 304 | fprintf(f, "[%08x] ", qopt->mtu); |
| 305 | } |
| 306 | } |
| 307 | |
Yang Yingliang | ddc6243 | 2013-11-26 09:55:35 +0800 | [diff] [blame] | 308 | latency = TIME_UNITS_PER_SEC*(qopt->limit/(double)rate64) - tc_core_tick2time(qopt->buffer); |
| 309 | if (prate64) { |
| 310 | double lat2 = TIME_UNITS_PER_SEC*(qopt->limit/(double)prate64) - tc_core_tick2time(qopt->mtu); |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 311 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 312 | if (lat2 > latency) |
| 313 | latency = lat2; |
| 314 | } |
Sergey V. Lobanov | 3ff10e8 | 2014-05-11 03:01:14 +0400 | [diff] [blame] | 315 | if (latency >= 0.0) |
| 316 | fprintf(f, "lat %s ", sprint_time(latency, b1)); |
| 317 | if (show_raw || latency < 0.0) |
| 318 | fprintf(f, "limit %s ", sprint_size(qopt->limit, b1)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 319 | |
Jesper Dangaard Brouer | 2c42579 | 2008-03-23 23:47:49 +0100 | [diff] [blame] | 320 | if (qopt->rate.overhead) { |
| 321 | fprintf(f, "overhead %d", qopt->rate.overhead); |
| 322 | } |
Jesper Dangaard Brouer | 3e92ff5 | 2013-08-30 14:02:10 +0200 | [diff] [blame] | 323 | linklayer = (qopt->rate.linklayer & TC_LINKLAYER_MASK); |
| 324 | if (linklayer > TC_LINKLAYER_ETHERNET || show_details) |
| 325 | fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b3)); |
Jesper Dangaard Brouer | 2c42579 | 2008-03-23 23:47:49 +0100 | [diff] [blame] | 326 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | |
net[shemminger]!kaber | 95812b5 | 2004-09-28 18:35:49 +0000 | [diff] [blame] | 330 | struct qdisc_util tbf_qdisc_util = { |
osdl.net!shemminger | f2f99e2 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 331 | .id = "tbf", |
| 332 | .parse_qopt = tbf_parse_opt, |
| 333 | .print_qopt = tbf_print_opt, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 334 | }; |