2004-04-16 04:56:59 +08:00
|
|
|
/*
|
|
|
|
* q_tbf.c TBF.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "utils.h"
|
|
|
|
#include "tc_util.h"
|
|
|
|
|
|
|
|
static void explain(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: ... tbf limit BYTES burst BYTES[/BYTES] rate KBPS [ mtu BYTES[/BYTES] ]\n");
|
2008-03-24 06:47:49 +08:00
|
|
|
fprintf(stderr, " [ peakrate KBPS ] [ latency TIME ] ");
|
2008-04-10 05:01:01 +08:00
|
|
|
fprintf(stderr, "[ overhead BYTES ] [ linklayer TYPE ]\n");
|
2004-04-16 04:56:59 +08:00
|
|
|
}
|
|
|
|
|
2013-02-19 15:46:04 +08:00
|
|
|
static void explain1(const char *arg, const char *val)
|
2004-04-16 04:56:59 +08:00
|
|
|
{
|
2013-02-19 15:46:04 +08:00
|
|
|
fprintf(stderr, "tbf: illegal value for \"%s\": \"%s\"\n", arg, val);
|
2004-04-16 04:56:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-25 03:31:36 +08:00
|
|
|
static int tbf_parse_opt(struct qdisc_util *qu, int argc, char **argv,
|
|
|
|
struct nlmsghdr *n, const char *dev)
|
2004-04-16 04:56:59 +08:00
|
|
|
{
|
2016-03-22 02:48:36 +08:00
|
|
|
int ok = 0;
|
2016-07-18 22:48:42 +08:00
|
|
|
struct tc_tbf_qopt opt = {};
|
2004-04-16 04:56:59 +08:00
|
|
|
__u32 rtab[256];
|
|
|
|
__u32 ptab[256];
|
2016-03-22 02:48:36 +08:00
|
|
|
unsigned buffer = 0, mtu = 0, mpu = 0, latency = 0;
|
|
|
|
int Rcell_log = -1, Pcell_log = -1;
|
|
|
|
unsigned short overhead = 0;
|
2008-04-10 05:01:01 +08:00
|
|
|
unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */
|
2004-04-16 04:56:59 +08:00
|
|
|
struct rtattr *tail;
|
2013-11-26 09:55:35 +08:00
|
|
|
__u64 rate64 = 0, prate64 = 0;
|
2004-04-16 04:56:59 +08:00
|
|
|
|
|
|
|
while (argc > 0) {
|
|
|
|
if (matches(*argv, "limit") == 0) {
|
|
|
|
NEXT_ARG();
|
2013-02-19 15:46:04 +08:00
|
|
|
if (opt.limit) {
|
|
|
|
fprintf(stderr, "tbf: duplicate \"limit\" specification\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (latency) {
|
|
|
|
fprintf(stderr, "tbf: specifying both \"latency\" and \"limit\" is not allowed\n");
|
2004-04-16 04:56:59 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (get_size(&opt.limit, *argv)) {
|
2013-02-19 15:46:04 +08:00
|
|
|
explain1("limit", *argv);
|
2004-04-16 04:56:59 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ok++;
|
|
|
|
} else if (matches(*argv, "latency") == 0) {
|
|
|
|
NEXT_ARG();
|
2013-02-19 15:46:04 +08:00
|
|
|
if (latency) {
|
|
|
|
fprintf(stderr, "tbf: duplicate \"latency\" specification\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (opt.limit) {
|
|
|
|
fprintf(stderr, "tbf: specifying both \"limit\" and \"/latency\" is not allowed\n");
|
2004-04-16 04:56:59 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2007-03-05 03:15:00 +08:00
|
|
|
if (get_time(&latency, *argv)) {
|
2013-02-19 15:46:04 +08:00
|
|
|
explain1("latency", *argv);
|
2004-04-16 04:56:59 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ok++;
|
|
|
|
} else if (matches(*argv, "burst") == 0 ||
|
|
|
|
strcmp(*argv, "buffer") == 0 ||
|
|
|
|
strcmp(*argv, "maxburst") == 0) {
|
2013-02-19 15:46:04 +08:00
|
|
|
const char *parm_name = *argv;
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2004-04-16 04:56:59 +08:00
|
|
|
NEXT_ARG();
|
|
|
|
if (buffer) {
|
2013-02-19 15:46:04 +08:00
|
|
|
fprintf(stderr, "tbf: duplicate \"buffer/burst/maxburst\" specification\n");
|
2004-04-16 04:56:59 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (get_size_and_cell(&buffer, &Rcell_log, *argv) < 0) {
|
2013-02-19 15:46:04 +08:00
|
|
|
explain1(parm_name, *argv);
|
2004-04-16 04:56:59 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ok++;
|
|
|
|
} else if (strcmp(*argv, "mtu") == 0 ||
|
|
|
|
strcmp(*argv, "minburst") == 0) {
|
2013-02-19 15:46:04 +08:00
|
|
|
const char *parm_name = *argv;
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2004-04-16 04:56:59 +08:00
|
|
|
NEXT_ARG();
|
|
|
|
if (mtu) {
|
2013-02-19 15:46:04 +08:00
|
|
|
fprintf(stderr, "tbf: duplicate \"mtu/minburst\" specification\n");
|
2004-04-16 04:56:59 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (get_size_and_cell(&mtu, &Pcell_log, *argv) < 0) {
|
2013-02-19 15:46:04 +08:00
|
|
|
explain1(parm_name, *argv);
|
2004-04-16 04:56:59 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ok++;
|
|
|
|
} else if (strcmp(*argv, "mpu") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
if (mpu) {
|
2013-02-19 15:46:04 +08:00
|
|
|
fprintf(stderr, "tbf: duplicate \"mpu\" specification\n");
|
2004-04-16 04:56:59 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (get_size(&mpu, *argv)) {
|
2013-02-19 15:46:04 +08:00
|
|
|
explain1("mpu", *argv);
|
2004-04-16 04:56:59 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ok++;
|
|
|
|
} else if (strcmp(*argv, "rate") == 0) {
|
|
|
|
NEXT_ARG();
|
2013-11-26 09:55:35 +08:00
|
|
|
if (rate64) {
|
2013-02-19 15:46:04 +08:00
|
|
|
fprintf(stderr, "tbf: duplicate \"rate\" specification\n");
|
2004-04-16 04:56:59 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2017-11-21 10:20:47 +08:00
|
|
|
if (strchr(*argv, '%')) {
|
|
|
|
if (get_percent_rate64(&rate64, *argv, dev)) {
|
|
|
|
explain1("rate", *argv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (get_rate64(&rate64, *argv)) {
|
2013-02-19 15:46:04 +08:00
|
|
|
explain1("rate", *argv);
|
2004-04-16 04:56:59 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ok++;
|
|
|
|
} else if (matches(*argv, "peakrate") == 0) {
|
|
|
|
NEXT_ARG();
|
2013-11-26 09:55:35 +08:00
|
|
|
if (prate64) {
|
2013-02-19 15:46:04 +08:00
|
|
|
fprintf(stderr, "tbf: duplicate \"peakrate\" specification\n");
|
2004-04-16 04:56:59 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2017-11-21 10:20:47 +08:00
|
|
|
if (strchr(*argv, '%')) {
|
|
|
|
if (get_percent_rate64(&prate64, *argv, dev)) {
|
|
|
|
explain1("peakrate", *argv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (get_rate64(&prate64, *argv)) {
|
2013-02-19 15:46:04 +08:00
|
|
|
explain1("peakrate", *argv);
|
2004-04-16 04:56:59 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ok++;
|
2008-03-24 06:47:49 +08:00
|
|
|
} else if (matches(*argv, "overhead") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
if (overhead) {
|
2013-02-19 15:46:04 +08:00
|
|
|
fprintf(stderr, "tbf: duplicate \"overhead\" specification\n");
|
2008-03-24 06:47:49 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (get_u16(&overhead, *argv, 10)) {
|
2013-02-19 15:46:04 +08:00
|
|
|
explain1("overhead", *argv); return -1;
|
2008-03-24 06:47:49 +08:00
|
|
|
}
|
2008-04-10 05:01:01 +08:00
|
|
|
} else if (matches(*argv, "linklayer") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
if (get_linklayer(&linklayer, *argv)) {
|
2013-02-19 15:46:04 +08:00
|
|
|
explain1("linklayer", *argv); return -1;
|
2008-04-10 05:01:01 +08:00
|
|
|
}
|
2004-04-16 04:56:59 +08:00
|
|
|
} else if (strcmp(*argv, "help") == 0) {
|
|
|
|
explain();
|
|
|
|
return -1;
|
|
|
|
} else {
|
2013-02-19 15:46:04 +08:00
|
|
|
fprintf(stderr, "tbf: unknown parameter \"%s\"\n", *argv);
|
2004-04-16 04:56:59 +08:00
|
|
|
explain();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
int verdict = 0;
|
2004-04-16 04:56:59 +08:00
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
/* Be nice to the user: try to emit all error messages in
|
|
|
|
* one go rather than reveal one more problem when a
|
|
|
|
* previous one has been fixed.
|
|
|
|
*/
|
2013-11-26 09:55:35 +08:00
|
|
|
if (rate64 == 0) {
|
2013-02-19 15:46:04 +08:00
|
|
|
fprintf(stderr, "tbf: the \"rate\" parameter is mandatory.\n");
|
|
|
|
verdict = -1;
|
|
|
|
}
|
|
|
|
if (!buffer) {
|
|
|
|
fprintf(stderr, "tbf: the \"burst\" parameter is mandatory.\n");
|
|
|
|
verdict = -1;
|
2004-04-16 04:56:59 +08:00
|
|
|
}
|
2013-11-26 09:55:35 +08:00
|
|
|
if (prate64) {
|
2004-04-16 04:56:59 +08:00
|
|
|
if (!mtu) {
|
2013-02-19 15:46:04 +08:00
|
|
|
fprintf(stderr, "tbf: when \"peakrate\" is specified, \"mtu\" must also be specified.\n");
|
|
|
|
verdict = -1;
|
2004-04-16 04:56:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opt.limit == 0 && latency == 0) {
|
2013-02-19 15:46:04 +08:00
|
|
|
fprintf(stderr, "tbf: either \"limit\" or \"latency\" is required.\n");
|
|
|
|
verdict = -1;
|
2004-04-16 04:56:59 +08:00
|
|
|
}
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
if (verdict != 0) {
|
|
|
|
explain();
|
|
|
|
return verdict;
|
|
|
|
}
|
2013-02-19 15:46:04 +08:00
|
|
|
|
2013-11-26 09:55:35 +08:00
|
|
|
opt.rate.rate = (rate64 >= (1ULL << 32)) ? ~0U : rate64;
|
|
|
|
opt.peakrate.rate = (prate64 >= (1ULL << 32)) ? ~0U : prate64;
|
|
|
|
|
2004-04-16 04:56:59 +08:00
|
|
|
if (opt.limit == 0) {
|
2013-11-26 09:55:35 +08:00
|
|
|
double lim = rate64*(double)latency/TIME_UNITS_PER_SEC + buffer;
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2013-11-26 09:55:35 +08:00
|
|
|
if (prate64) {
|
|
|
|
double lim2 = prate64*(double)latency/TIME_UNITS_PER_SEC + mtu;
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2004-04-16 04:56:59 +08:00
|
|
|
if (lim2 < lim)
|
|
|
|
lim = lim2;
|
|
|
|
}
|
|
|
|
opt.limit = lim;
|
|
|
|
}
|
|
|
|
|
2008-03-24 06:47:49 +08:00
|
|
|
opt.rate.mpu = mpu;
|
|
|
|
opt.rate.overhead = overhead;
|
2008-04-10 05:01:01 +08:00
|
|
|
if (tc_calc_rtable(&opt.rate, rtab, Rcell_log, mtu, linklayer) < 0) {
|
2013-02-19 15:46:04 +08:00
|
|
|
fprintf(stderr, "tbf: failed to calculate rate table.\n");
|
2004-04-16 04:56:59 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
opt.buffer = tc_calc_xmittime(opt.rate.rate, buffer);
|
2007-09-05 16:47:47 +08:00
|
|
|
|
2004-04-16 04:56:59 +08:00
|
|
|
if (opt.peakrate.rate) {
|
2008-03-24 06:47:49 +08:00
|
|
|
opt.peakrate.mpu = mpu;
|
|
|
|
opt.peakrate.overhead = overhead;
|
2008-04-10 05:01:01 +08:00
|
|
|
if (tc_calc_rtable(&opt.peakrate, ptab, Pcell_log, mtu, linklayer) < 0) {
|
2013-02-19 15:46:04 +08:00
|
|
|
fprintf(stderr, "tbf: failed to calculate peak rate table.\n");
|
2004-04-16 04:56:59 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
opt.mtu = tc_calc_xmittime(opt.peakrate.rate, mtu);
|
|
|
|
}
|
|
|
|
|
2018-01-31 16:15:08 +08:00
|
|
|
tail = addattr_nest(n, 1024, TCA_OPTIONS);
|
2004-04-16 04:56:59 +08:00
|
|
|
addattr_l(n, 2024, TCA_TBF_PARMS, &opt, sizeof(opt));
|
2014-01-16 11:09:13 +08:00
|
|
|
addattr_l(n, 2124, TCA_TBF_BURST, &buffer, sizeof(buffer));
|
2013-11-26 09:55:35 +08:00
|
|
|
if (rate64 >= (1ULL << 32))
|
|
|
|
addattr_l(n, 2124, TCA_TBF_RATE64, &rate64, sizeof(rate64));
|
2004-04-16 04:56:59 +08:00
|
|
|
addattr_l(n, 3024, TCA_TBF_RTAB, rtab, 1024);
|
2013-11-26 09:55:35 +08:00
|
|
|
if (opt.peakrate.rate) {
|
|
|
|
if (prate64 >= (1ULL << 32))
|
|
|
|
addattr_l(n, 3124, TCA_TBF_PRATE64, &prate64, sizeof(prate64));
|
2014-01-16 11:09:13 +08:00
|
|
|
addattr_l(n, 3224, TCA_TBF_PBURST, &mtu, sizeof(mtu));
|
2004-04-16 04:56:59 +08:00
|
|
|
addattr_l(n, 4096, TCA_TBF_PTAB, ptab, 1024);
|
2013-11-26 09:55:35 +08:00
|
|
|
}
|
2018-01-31 16:15:08 +08:00
|
|
|
addattr_nest_end(n, tail);
|
2004-04-16 04:56:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tbf_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
|
|
|
|
{
|
2013-11-26 09:55:35 +08:00
|
|
|
struct rtattr *tb[TCA_TBF_MAX+1];
|
2004-04-16 04:56:59 +08:00
|
|
|
struct tc_tbf_qopt *qopt;
|
2013-08-30 20:02:10 +08:00
|
|
|
unsigned int linklayer;
|
2004-04-16 04:56:59 +08:00
|
|
|
double buffer, mtu;
|
|
|
|
double latency;
|
2013-11-26 09:55:35 +08:00
|
|
|
__u64 rate64 = 0, prate64 = 0;
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2004-04-16 04:56:59 +08:00
|
|
|
SPRINT_BUF(b1);
|
|
|
|
SPRINT_BUF(b2);
|
2013-08-30 20:02:10 +08:00
|
|
|
SPRINT_BUF(b3);
|
2004-04-16 04:56:59 +08:00
|
|
|
|
|
|
|
if (opt == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2013-11-26 09:55:35 +08:00
|
|
|
parse_rtattr_nested(tb, TCA_TBF_MAX, opt);
|
2004-04-16 04:56:59 +08:00
|
|
|
|
|
|
|
if (tb[TCA_TBF_PARMS] == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
qopt = RTA_DATA(tb[TCA_TBF_PARMS]);
|
|
|
|
if (RTA_PAYLOAD(tb[TCA_TBF_PARMS]) < sizeof(*qopt))
|
|
|
|
return -1;
|
2013-11-26 09:55:35 +08:00
|
|
|
rate64 = qopt->rate.rate;
|
|
|
|
if (tb[TCA_TBF_RATE64] &&
|
|
|
|
RTA_PAYLOAD(tb[TCA_TBF_RATE64]) >= sizeof(rate64))
|
|
|
|
rate64 = rta_getattr_u64(tb[TCA_TBF_RATE64]);
|
|
|
|
fprintf(f, "rate %s ", sprint_rate(rate64, b1));
|
|
|
|
buffer = tc_calc_xmitsize(rate64, qopt->buffer);
|
2004-04-16 04:56:59 +08:00
|
|
|
if (show_details) {
|
|
|
|
fprintf(f, "burst %s/%u mpu %s ", sprint_size(buffer, b1),
|
|
|
|
1<<qopt->rate.cell_log, sprint_size(qopt->rate.mpu, b2));
|
|
|
|
} else {
|
|
|
|
fprintf(f, "burst %s ", sprint_size(buffer, b1));
|
|
|
|
}
|
|
|
|
if (show_raw)
|
|
|
|
fprintf(f, "[%08x] ", qopt->buffer);
|
2013-11-26 09:55:35 +08:00
|
|
|
prate64 = qopt->peakrate.rate;
|
|
|
|
if (tb[TCA_TBF_PRATE64] &&
|
|
|
|
RTA_PAYLOAD(tb[TCA_TBF_PRATE64]) >= sizeof(prate64))
|
|
|
|
prate64 = rta_getattr_u64(tb[TCA_TBF_PRATE64]);
|
|
|
|
if (prate64) {
|
|
|
|
fprintf(f, "peakrate %s ", sprint_rate(prate64, b1));
|
2004-04-16 04:56:59 +08:00
|
|
|
if (qopt->mtu || qopt->peakrate.mpu) {
|
2013-11-26 09:55:35 +08:00
|
|
|
mtu = tc_calc_xmitsize(prate64, qopt->mtu);
|
2004-04-16 04:56:59 +08:00
|
|
|
if (show_details) {
|
|
|
|
fprintf(f, "mtu %s/%u mpu %s ", sprint_size(mtu, b1),
|
|
|
|
1<<qopt->peakrate.cell_log, sprint_size(qopt->peakrate.mpu, b2));
|
|
|
|
} else {
|
|
|
|
fprintf(f, "minburst %s ", sprint_size(mtu, b1));
|
|
|
|
}
|
|
|
|
if (show_raw)
|
|
|
|
fprintf(f, "[%08x] ", qopt->mtu);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-26 09:55:35 +08:00
|
|
|
latency = TIME_UNITS_PER_SEC*(qopt->limit/(double)rate64) - tc_core_tick2time(qopt->buffer);
|
|
|
|
if (prate64) {
|
|
|
|
double lat2 = TIME_UNITS_PER_SEC*(qopt->limit/(double)prate64) - tc_core_tick2time(qopt->mtu);
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2004-04-16 04:56:59 +08:00
|
|
|
if (lat2 > latency)
|
|
|
|
latency = lat2;
|
|
|
|
}
|
2014-05-11 07:01:14 +08:00
|
|
|
if (latency >= 0.0)
|
|
|
|
fprintf(f, "lat %s ", sprint_time(latency, b1));
|
|
|
|
if (show_raw || latency < 0.0)
|
|
|
|
fprintf(f, "limit %s ", sprint_size(qopt->limit, b1));
|
2004-04-16 04:56:59 +08:00
|
|
|
|
2008-03-24 06:47:49 +08:00
|
|
|
if (qopt->rate.overhead) {
|
|
|
|
fprintf(f, "overhead %d", qopt->rate.overhead);
|
|
|
|
}
|
2013-08-30 20:02:10 +08:00
|
|
|
linklayer = (qopt->rate.linklayer & TC_LINKLAYER_MASK);
|
|
|
|
if (linklayer > TC_LINKLAYER_ETHERNET || show_details)
|
|
|
|
fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b3));
|
2008-03-24 06:47:49 +08:00
|
|
|
|
2004-04-16 04:56:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-09-29 02:35:49 +08:00
|
|
|
struct qdisc_util tbf_qdisc_util = {
|
2004-09-01 01:45:21 +08:00
|
|
|
.id = "tbf",
|
|
|
|
.parse_qopt = tbf_parse_opt,
|
|
|
|
.print_qopt = tbf_print_opt,
|
2004-04-16 04:56:59 +08:00
|
|
|
};
|