]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - tc/m_police.c
Merge branch 'main' into next
[mirror_iproute2.git] / tc / m_police.c
index 86117db0482ec93486d4b2f79c09cea8ddca90ae..83b25db49f217f3b35f3baedbae219461d341ae1 100644 (file)
@@ -16,7 +16,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <syslog.h>
 #include <fcntl.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include "utils.h"
 #include "tc_util.h"
 
+static int act_parse_police(struct action_util *a, int *argc_p,
+                           char ***argv_p, int tca_id, struct nlmsghdr *n);
+static int print_police(struct action_util *a, FILE *f, struct rtattr *tb);
+
 struct action_util police_action_util = {
        .id = "police",
        .parse_aopt = act_parse_police,
@@ -34,25 +37,20 @@ struct action_util police_action_util = {
 
 static void usage(void)
 {
-       fprintf(stderr, "Usage: ... police rate BPS burst BYTES[/BYTES] [ mtu BYTES[/BYTES] ]\n");
-       fprintf(stderr, "                [ peakrate BPS ] [ avrate BPS ] [ overhead BYTES ]\n");
-       fprintf(stderr, "                [ linklayer TYPE ] [ CONTROL ]\n");
-
-       fprintf(stderr, "Where: CONTROL := conform-exceed <EXCEEDACT>[/NOTEXCEEDACT]\n");
-       fprintf(stderr, "                  Define how to handle packets which exceed (<EXCEEDACT>)\n");
-       fprintf(stderr, "                  or conform (<NOTEXCEEDACT>) the configured bandwidth limit.\n");
-       fprintf(stderr, "       EXCEEDACT/NOTEXCEEDACT := { pipe | ok | reclassify | drop | continue |\n");
-       fprintf(stderr, "                                   goto chain <CHAIN_INDEX> }\n");
+       fprintf(stderr,
+               "Usage: ... police rate BPS burst BYTES[/BYTES] [ mtu BYTES[/BYTES] ]\n"
+               "               [ peakrate BPS ] [ avrate BPS ] [ overhead BYTES ]\n"
+               "               [ linklayer TYPE ] [ CONTROL ]\n"
+               "Where: CONTROL := conform-exceed <EXCEEDACT>[/NOTEXCEEDACT]\n"
+               "                 Define how to handle packets which exceed (<EXCEEDACT>)\n"
+               "                 or conform (<NOTEXCEEDACT>) the configured bandwidth limit.\n"
+               "       EXCEEDACT/NOTEXCEEDACT := { pipe | ok | reclassify | drop | continue |\n"
+               "                                  goto chain <CHAIN_INDEX> }\n");
        exit(-1);
 }
 
-static void explain1(char *arg)
-{
-       fprintf(stderr, "Illegal \"%s\"\n", arg);
-}
-
-int act_parse_police(struct action_util *a, int *argc_p, char ***argv_p,
-                    int tca_id, struct nlmsghdr *n)
+static int act_parse_police(struct action_util *a, int *argc_p, char ***argv_p,
+                           int tca_id, struct nlmsghdr *n)
 {
        int argc = *argc_p;
        char **argv = *argv_p;
@@ -68,6 +66,7 @@ int act_parse_police(struct action_util *a, int *argc_p, char ***argv_p,
        unsigned int linklayer = LINKLAYER_ETHERNET; /* Assume ethernet */
        int Rcell_log =  -1, Pcell_log = -1;
        struct rtattr *tail;
+       __u64 rate64 = 0, prate64 = 0;
 
        if (a) /* new way of doing things */
                NEXT_ARG();
@@ -79,169 +78,152 @@ int act_parse_police(struct action_util *a, int *argc_p, char ***argv_p,
 
                if (matches(*argv, "index") == 0) {
                        NEXT_ARG();
-                       if (get_u32(&p.index, *argv, 10)) {
-                               fprintf(stderr, "Illegal \"index\"\n");
-                               return -1;
-                       }
+                       if (get_u32(&p.index, *argv, 10))
+                               invarg("index", *argv);
                } else if (matches(*argv, "burst") == 0 ||
                        strcmp(*argv, "buffer") == 0 ||
                        strcmp(*argv, "maxburst") == 0) {
                        NEXT_ARG();
-                       if (buffer) {
-                               fprintf(stderr, "Double \"buffer/burst\" spec\n");
-                               return -1;
-                       }
-                       if (get_size_and_cell(&buffer, &Rcell_log, *argv) < 0) {
-                               explain1("buffer");
-                               return -1;
-                       }
+                       if (buffer)
+                               duparg("buffer/burst", *argv);
+                       if (get_size_and_cell(&buffer, &Rcell_log, *argv) < 0)
+                               invarg("buffer", *argv);
                } else if (strcmp(*argv, "mtu") == 0 ||
                           strcmp(*argv, "minburst") == 0) {
                        NEXT_ARG();
-                       if (mtu) {
-                               fprintf(stderr, "Double \"mtu/minburst\" spec\n");
-                               return -1;
-                       }
-                       if (get_size_and_cell(&mtu, &Pcell_log, *argv) < 0) {
-                               explain1("mtu");
-                               return -1;
-                       }
+                       if (mtu)
+                               duparg("mtu/minburst", *argv);
+                       if (get_size_and_cell(&mtu, &Pcell_log, *argv) < 0)
+                               invarg("mtu", *argv);
                } else if (strcmp(*argv, "mpu") == 0) {
                        NEXT_ARG();
-                       if (mpu) {
-                               fprintf(stderr, "Double \"mpu\" spec\n");
-                               return -1;
-                       }
-                       if (get_size(&mpu, *argv)) {
-                               explain1("mpu");
-                               return -1;
-                       }
+                       if (mpu)
+                               duparg("mpu", *argv);
+                       if (get_size(&mpu, *argv))
+                               invarg("mpu", *argv);
                } else if (strcmp(*argv, "rate") == 0) {
                        NEXT_ARG();
-                       if (p.rate.rate) {
-                               fprintf(stderr, "Double \"rate\" spec\n");
-                               return -1;
-                       }
-                       if (get_rate(&p.rate.rate, *argv)) {
-                               explain1("rate");
-                               return -1;
-                       }
+                       if (rate64)
+                               duparg("rate", *argv);
+                       if (get_rate64(&rate64, *argv))
+                               invarg("rate", *argv);
                } else if (strcmp(*argv, "avrate") == 0) {
                        NEXT_ARG();
-                       if (avrate) {
-                               fprintf(stderr, "Double \"avrate\" spec\n");
-                               return -1;
-                       }
-                       if (get_rate(&avrate, *argv)) {
-                               explain1("avrate");
-                               return -1;
-                       }
+                       if (avrate)
+                               duparg("avrate", *argv);
+                       if (get_rate(&avrate, *argv))
+                               invarg("avrate", *argv);
                } else if (matches(*argv, "peakrate") == 0) {
                        NEXT_ARG();
-                       if (p.peakrate.rate) {
-                               fprintf(stderr, "Double \"peakrate\" spec\n");
-                               return -1;
-                       }
-                       if (get_rate(&p.peakrate.rate, *argv)) {
-                               explain1("peakrate");
-                               return -1;
-                       }
+                       if (prate64)
+                               duparg("peakrate", *argv);
+                       if (get_rate64(&prate64, *argv))
+                               invarg("peakrate", *argv);
                } else if (matches(*argv, "reclassify") == 0 ||
                           matches(*argv, "drop") == 0 ||
                           matches(*argv, "shot") == 0 ||
                           matches(*argv, "continue") == 0 ||
                           matches(*argv, "pass") == 0 ||
+                          matches(*argv, "ok") == 0 ||
                           matches(*argv, "pipe") == 0 ||
                           matches(*argv, "goto") == 0) {
-                       if (parse_action_control(&argc, &argv, &p.action, false))
-                               return -1;
+                       if (!parse_action_control(&argc, &argv, &p.action, false))
+                               goto action_ctrl_ok;
+                       return -1;
                } else if (strcmp(*argv, "conform-exceed") == 0) {
                        NEXT_ARG();
-                       if (parse_action_control_slash(&argc, &argv, &p.action,
-                                                      &presult, true))
-                               return -1;
+                       if (!parse_action_control_slash(&argc, &argv, &p.action,
+                                                       &presult, true))
+                               goto action_ctrl_ok;
+                       return -1;
                } else if (matches(*argv, "overhead") == 0) {
                        NEXT_ARG();
-                       if (get_u16(&overhead, *argv, 10)) {
-                               explain1("overhead"); return -1;
-                       }
+                       if (get_u16(&overhead, *argv, 10))
+                               invarg("overhead", *argv);
                } else if (matches(*argv, "linklayer") == 0) {
                        NEXT_ARG();
-                       if (get_linklayer(&linklayer, *argv)) {
-                               explain1("linklayer"); return -1;
-                       }
+                       if (get_linklayer(&linklayer, *argv))
+                               invarg("linklayer", *argv);
                } else if (strcmp(*argv, "help") == 0) {
                        usage();
                } else {
                        break;
                }
+               NEXT_ARG_FWD();
+action_ctrl_ok:
                ok++;
-               argc--; argv++;
        }
 
        if (!ok)
                return -1;
 
-       if (p.rate.rate && avrate)
+       if (rate64 && 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");
+       if (!rate64 && !avrate && !p.index && !mtu) {
+               fprintf(stderr, "'rate' or 'avrate' or 'mtu' 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");
+       if (rate64 && !buffer && !avrate) {
+               fprintf(stderr, "'burst' requires 'rate'.\n");
                return -1;
        }
 
-       if (p.peakrate.rate) {
-               if (!p.rate.rate) {
-                       fprintf(stderr, "\"peakrate\" requires \"rate\".\n");
+       if (prate64) {
+               if (!rate64) {
+                       fprintf(stderr, "'peakrate' requires 'rate'.\n");
                        return -1;
                }
                if (!mtu) {
-                       fprintf(stderr, "\"mtu\" is required, if \"peakrate\" is requested.\n");
+                       fprintf(stderr, "'mtu' is required, if 'peakrate' is requested.\n");
                        return -1;
                }
        }
 
-       if (p.rate.rate) {
+       if (rate64) {
+               p.rate.rate = (rate64 >= (1ULL << 32)) ? ~0U : rate64;
                p.rate.mpu = mpu;
                p.rate.overhead = overhead;
-               if (tc_calc_rtable(&p.rate, rtab, Rcell_log, mtu,
-                                  linklayer) < 0) {
+               if (tc_calc_rtable_64(&p.rate, rtab, Rcell_log, mtu,
+                                  linklayer, rate64) < 0) {
                        fprintf(stderr, "POLICE: failed to calculate rate table.\n");
                        return -1;
                }
-               p.burst = tc_calc_xmittime(p.rate.rate, buffer);
+               p.burst = tc_calc_xmittime(rate64, buffer);
        }
        p.mtu = mtu;
-       if (p.peakrate.rate) {
+       if (prate64) {
+               p.peakrate.rate = (prate64 >= (1ULL << 32)) ? ~0U : prate64;
                p.peakrate.mpu = mpu;
                p.peakrate.overhead = overhead;
-               if (tc_calc_rtable(&p.peakrate, ptab, Pcell_log, mtu,
-                                  linklayer) < 0) {
+               if (tc_calc_rtable_64(&p.peakrate, ptab, Pcell_log, mtu,
+                                  linklayer, prate64) < 0) {
                        fprintf(stderr, "POLICE: failed to calculate peak rate table.\n");
                        return -1;
                }
        }
 
-       tail = NLMSG_TAIL(n);
-       addattr_l(n, MAX_MSG, tca_id, NULL, 0);
+       tail = addattr_nest(n, MAX_MSG, tca_id);
        addattr_l(n, MAX_MSG, TCA_POLICE_TBF, &p, sizeof(p));
-       if (p.rate.rate)
+       if (rate64) {
                addattr_l(n, MAX_MSG, TCA_POLICE_RATE, rtab, 1024);
-       if (p.peakrate.rate)
+               if (rate64 >= (1ULL << 32))
+                       addattr64(n, MAX_MSG, TCA_POLICE_RATE64, rate64);
+       }
+       if (prate64) {
                addattr_l(n, MAX_MSG, TCA_POLICE_PEAKRATE, ptab, 1024);
+               if (prate64 >= (1ULL << 32))
+                       addattr64(n, MAX_MSG, TCA_POLICE_PEAKRATE64, prate64);
+       }
        if (avrate)
                addattr32(n, MAX_MSG, TCA_POLICE_AVRATE, avrate);
        if (presult)
                addattr32(n, MAX_MSG, TCA_POLICE_RESULT, presult);
 
-       tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
+       addattr_nest_end(n, tail);
        res = 0;
 
        *argc_p = argc;
@@ -254,7 +236,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);
 }
 
-int print_police(struct action_util *a, FILE *f, struct rtattr *arg)
+static int print_police(struct action_util *a, FILE *f, struct rtattr *arg)
 {
        SPRINT_BUF(b1);
        SPRINT_BUF(b2);
@@ -262,6 +244,7 @@ int print_police(struct action_util *a, FILE *f, struct rtattr *arg)
        struct rtattr *tb[TCA_POLICE_MAX+1];
        unsigned int buffer;
        unsigned int linklayer;
+       __u64 rate64, prate64;
 
        if (arg == NULL)
                return 0;
@@ -280,16 +263,26 @@ int print_police(struct action_util *a, FILE *f, struct rtattr *arg)
 #endif
        p = RTA_DATA(tb[TCA_POLICE_TBF]);
 
+       rate64 = p->rate.rate;
+       if (tb[TCA_POLICE_RATE64] &&
+           RTA_PAYLOAD(tb[TCA_POLICE_RATE64]) >= sizeof(rate64))
+               rate64 = rta_getattr_u64(tb[TCA_POLICE_RATE64]);
+
        fprintf(f, " police 0x%x ", p->index);
-       fprintf(f, "rate %s ", sprint_rate(p->rate.rate, b1));
-       buffer = tc_calc_xmitsize(p->rate.rate, p->burst);
+       fprintf(f, "rate %s ", sprint_rate(rate64, b1));
+       buffer = tc_calc_xmitsize(rate64, p->burst);
        fprintf(f, "burst %s ", sprint_size(buffer, b1));
        fprintf(f, "mtu %s ", sprint_size(p->mtu, b1));
        if (show_raw)
                fprintf(f, "[%08x] ", p->burst);
 
-       if (p->peakrate.rate)
-               fprintf(f, "peakrate %s ", sprint_rate(p->peakrate.rate, b1));
+       prate64 = p->peakrate.rate;
+       if (tb[TCA_POLICE_PEAKRATE64] &&
+           RTA_PAYLOAD(tb[TCA_POLICE_PEAKRATE64]) >= sizeof(prate64))
+               prate64 = rta_getattr_u64(tb[TCA_POLICE_PEAKRATE64]);
+
+       if (prate64)
+               fprintf(f, "peakrate %s ", sprint_rate(prate64, b1));
 
        if (tb[TCA_POLICE_AVRATE])
                fprintf(f, "avrate %s ",