tc action policer: Avoid nonsensical input

The user must at least specify a choice of the token bucket or
ewma policing or late binding index. TB policing requires at minimal
a rate and burst.

In addition fix formatting issues (80 chars etc).

Signed-off-by: Jamal Hadi Salim <jhs@mojatatu.com>
This commit is contained in:
Jamal Hadi Salim 2016-05-25 06:05:48 -04:00 committed by Stephen Hemminger
parent 57bdf8b764
commit 45c6837911
1 changed files with 30 additions and 15 deletions

View File

@ -99,7 +99,6 @@ static int police_action_a2n(const char *arg, int *result)
return 0; return 0;
} }
static int get_police_result(int *action, int *result, char *arg) static int get_police_result(int *action, int *result, char *arg)
{ {
char *p = strchr(arg, '/'); char *p = strchr(arg, '/');
@ -121,8 +120,8 @@ static int get_police_result(int *action, int *result, char *arg)
return 0; return 0;
} }
int act_parse_police(struct action_util *a, int *argc_p, char ***argv_p,
int act_parse_police(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n) int tca_id, struct nlmsghdr *n)
{ {
int argc = *argc_p; int argc = *argc_p;
char **argv = *argv_p; char **argv = *argv_p;
@ -258,10 +257,21 @@ int act_parse_police(struct action_util *a, int *argc_p, char ***argv_p, int tca
if (!ok) if (!ok)
return -1; return -1;
if (p.rate.rate && !buffer) { if (p.rate.rate && avrate)
return -1;
/* Must at least do late binding, use TB or ewma policing */
if (!p.rate.rate && !avrate && !p.index) {
fprintf(stderr, "\"rate\" or \"avrate\" MUST be specified.\n");
return -1;
}
/* When the TB policer is used, burst is required */
if (p.rate.rate && !buffer && !avrate) {
fprintf(stderr, "\"burst\" requires \"rate\".\n"); fprintf(stderr, "\"burst\" requires \"rate\".\n");
return -1; return -1;
} }
if (p.peakrate.rate) { if (p.peakrate.rate) {
if (!p.rate.rate) { if (!p.rate.rate) {
fprintf(stderr, "\"peakrate\" requires \"rate\".\n"); fprintf(stderr, "\"peakrate\" requires \"rate\".\n");
@ -276,8 +286,9 @@ int act_parse_police(struct action_util *a, int *argc_p, char ***argv_p, int tca
if (p.rate.rate) { if (p.rate.rate) {
p.rate.mpu = mpu; p.rate.mpu = mpu;
p.rate.overhead = overhead; p.rate.overhead = overhead;
if (tc_calc_rtable(&p.rate, rtab, Rcell_log, mtu, linklayer) < 0) { if (tc_calc_rtable(&p.rate, rtab, Rcell_log, mtu,
fprintf(stderr, "TBF: failed to calculate rate table.\n"); linklayer) < 0) {
fprintf(stderr, "POLICE: failed to calculate rate table.\n");
return -1; return -1;
} }
p.burst = tc_calc_xmittime(p.rate.rate, buffer); p.burst = tc_calc_xmittime(p.rate.rate, buffer);
@ -286,7 +297,8 @@ int act_parse_police(struct action_util *a, int *argc_p, char ***argv_p, int tca
if (p.peakrate.rate) { if (p.peakrate.rate) {
p.peakrate.mpu = mpu; p.peakrate.mpu = mpu;
p.peakrate.overhead = overhead; p.peakrate.overhead = overhead;
if (tc_calc_rtable(&p.peakrate, ptab, Pcell_log, mtu, linklayer) < 0) { if (tc_calc_rtable(&p.peakrate, ptab, Pcell_log, mtu,
linklayer) < 0) {
fprintf(stderr, "POLICE: failed to calculate peak rate table.\n"); fprintf(stderr, "POLICE: failed to calculate peak rate table.\n");
return -1; return -1;
} }
@ -317,8 +329,7 @@ int parse_police(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
return act_parse_police(NULL, argc_p, argv_p, tca_id, n); return act_parse_police(NULL, argc_p, argv_p, tca_id, n);
} }
int int print_police(struct action_util *a, FILE *f, struct rtattr *arg)
print_police(struct action_util *a, FILE *f, struct rtattr *arg)
{ {
SPRINT_BUF(b1); SPRINT_BUF(b1);
SPRINT_BUF(b2); SPRINT_BUF(b2);
@ -354,22 +365,26 @@ print_police(struct action_util *a, FILE *f, struct rtattr *arg)
if (p->peakrate.rate) if (p->peakrate.rate)
fprintf(f, "peakrate %s ", sprint_rate(p->peakrate.rate, b1)); fprintf(f, "peakrate %s ", sprint_rate(p->peakrate.rate, b1));
if (tb[TCA_POLICE_AVRATE]) if (tb[TCA_POLICE_AVRATE])
fprintf(f, "avrate %s ", sprint_rate(rta_getattr_u32(tb[TCA_POLICE_AVRATE]), b1)); fprintf(f, "avrate %s ",
fprintf(f, "action %s", police_action_n2a(p->action, b1, sizeof(b1))); sprint_rate(rta_getattr_u32(tb[TCA_POLICE_AVRATE]),
b1));
fprintf(f, "action %s",
police_action_n2a(p->action, b1, sizeof(b1)));
if (tb[TCA_POLICE_RESULT]) { if (tb[TCA_POLICE_RESULT]) {
fprintf(f, "/%s ", police_action_n2a(*(int *)RTA_DATA(tb[TCA_POLICE_RESULT]), b1, sizeof(b1))); fprintf(f, "/%s",
police_action_n2a(*(int *)RTA_DATA(tb[TCA_POLICE_RESULT]), b1, sizeof(b1)));
} else } else
fprintf(f, " "); fprintf(f, " ");
fprintf(f, "overhead %ub ", p->rate.overhead); fprintf(f, "overhead %ub ", p->rate.overhead);
linklayer = (p->rate.linklayer & TC_LINKLAYER_MASK); linklayer = (p->rate.linklayer & TC_LINKLAYER_MASK);
if (linklayer > TC_LINKLAYER_ETHERNET || show_details) if (linklayer > TC_LINKLAYER_ETHERNET || show_details)
fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b2)); fprintf(f, "linklayer %s ", sprint_linklayer(linklayer, b2));
fprintf(f, "\nref %d bind %d\n", p->refcnt, p->bindcnt); fprintf(f, "\n\tref %d bind %d\n", p->refcnt, p->bindcnt);
return 0; return 0;
} }
int int tc_print_police(FILE *f, struct rtattr *arg)
tc_print_police(FILE *f, struct rtattr *arg) { {
return print_police(&police_action_util, f, arg); return print_police(&police_action_util, f, arg);
} }