iproute2/dcb/dcb_maxrate.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2
   3#include <errno.h>
   4#include <inttypes.h>
   5#include <stdio.h>
   6#include <linux/dcbnl.h>
   7
   8#include "dcb.h"
   9#include "utils.h"
  10
  11static void dcb_maxrate_help_set(void)
  12{
  13        fprintf(stderr,
  14                "Usage: dcb maxrate set dev STRING\n"
  15                "           [ tc-maxrate RATE-MAP ]\n"
  16                "\n"
  17                " where RATE-MAP := [ RATE-MAP ] RATE-MAPPING\n"
  18                "       RATE-MAPPING := { all | TC }:RATE\n"
  19                "       TC := { 0 .. 7 }\n"
  20                "\n"
  21        );
  22}
  23
  24static void dcb_maxrate_help_show(void)
  25{
  26        fprintf(stderr,
  27                "Usage: dcb [ -i ] maxrate show dev STRING\n"
  28                "           [ tc-maxrate ]\n"
  29                "\n"
  30        );
  31}
  32
  33static void dcb_maxrate_help(void)
  34{
  35        fprintf(stderr,
  36                "Usage: dcb maxrate help\n"
  37                "\n"
  38        );
  39        dcb_maxrate_help_show();
  40        dcb_maxrate_help_set();
  41}
  42
  43static int dcb_maxrate_parse_mapping_tc_maxrate(__u32 key, char *value, void *data)
  44{
  45        __u64 rate;
  46
  47        if (get_rate64(&rate, value))
  48                return -EINVAL;
  49
  50        return dcb_parse_mapping("TC", key, IEEE_8021QAZ_MAX_TCS - 1,
  51                                 "RATE", rate, -1,
  52                                 dcb_set_u64, data);
  53}
  54
  55static void dcb_maxrate_print_tc_maxrate(struct dcb *dcb, const struct ieee_maxrate *maxrate)
  56{
  57        size_t size = ARRAY_SIZE(maxrate->tc_maxrate);
  58        SPRINT_BUF(b);
  59        size_t i;
  60
  61        open_json_array(PRINT_JSON, "tc_maxrate");
  62        print_string(PRINT_FP, NULL, "tc-maxrate ", NULL);
  63
  64        for (i = 0; i < size; i++) {
  65                snprintf(b, sizeof(b), "%zd:%%s ", i);
  66                print_rate(dcb->use_iec, PRINT_ANY, NULL, b, maxrate->tc_maxrate[i]);
  67        }
  68
  69        close_json_array(PRINT_JSON, "tc_maxrate");
  70}
  71
  72static void dcb_maxrate_print(struct dcb *dcb, const struct ieee_maxrate *maxrate)
  73{
  74        dcb_maxrate_print_tc_maxrate(dcb, maxrate);
  75        print_nl();
  76}
  77
  78static int dcb_maxrate_get(struct dcb *dcb, const char *dev, struct ieee_maxrate *maxrate)
  79{
  80        return dcb_get_attribute(dcb, dev, DCB_ATTR_IEEE_MAXRATE, maxrate, sizeof(*maxrate));
  81}
  82
  83static int dcb_maxrate_set(struct dcb *dcb, const char *dev, const struct ieee_maxrate *maxrate)
  84{
  85        return dcb_set_attribute(dcb, dev, DCB_ATTR_IEEE_MAXRATE, maxrate, sizeof(*maxrate));
  86}
  87
  88static int dcb_cmd_maxrate_set(struct dcb *dcb, const char *dev, int argc, char **argv)
  89{
  90        struct ieee_maxrate maxrate;
  91        int ret;
  92
  93        if (!argc) {
  94                dcb_maxrate_help_set();
  95                return 0;
  96        }
  97
  98        ret = dcb_maxrate_get(dcb, dev, &maxrate);
  99        if (ret)
 100                return ret;
 101
 102        do {
 103                if (matches(*argv, "help") == 0) {
 104                        dcb_maxrate_help_set();
 105                        return 0;
 106                } else if (matches(*argv, "tc-maxrate") == 0) {
 107                        NEXT_ARG();
 108                        ret = parse_mapping(&argc, &argv, true,
 109                                            &dcb_maxrate_parse_mapping_tc_maxrate, &maxrate);
 110                        if (ret) {
 111                                fprintf(stderr, "Invalid mapping %s\n", *argv);
 112                                return ret;
 113                        }
 114                        continue;
 115                } else {
 116                        fprintf(stderr, "What is \"%s\"?\n", *argv);
 117                        dcb_maxrate_help_set();
 118                        return -EINVAL;
 119                }
 120
 121                NEXT_ARG_FWD();
 122        } while (argc > 0);
 123
 124        return dcb_maxrate_set(dcb, dev, &maxrate);
 125}
 126
 127static int dcb_cmd_maxrate_show(struct dcb *dcb, const char *dev, int argc, char **argv)
 128{
 129        struct ieee_maxrate maxrate;
 130        int ret;
 131
 132        ret = dcb_maxrate_get(dcb, dev, &maxrate);
 133        if (ret)
 134                return ret;
 135
 136        open_json_object(NULL);
 137
 138        if (!argc) {
 139                dcb_maxrate_print(dcb, &maxrate);
 140                goto out;
 141        }
 142
 143        do {
 144                if (matches(*argv, "help") == 0) {
 145                        dcb_maxrate_help_show();
 146                        return 0;
 147                } else if (matches(*argv, "tc-maxrate") == 0) {
 148                        dcb_maxrate_print_tc_maxrate(dcb, &maxrate);
 149                        print_nl();
 150                } else {
 151                        fprintf(stderr, "What is \"%s\"?\n", *argv);
 152                        dcb_maxrate_help_show();
 153                        return -EINVAL;
 154                }
 155
 156                NEXT_ARG_FWD();
 157        } while (argc > 0);
 158
 159out:
 160        close_json_object();
 161        return 0;
 162}
 163
 164int dcb_cmd_maxrate(struct dcb *dcb, int argc, char **argv)
 165{
 166        if (!argc || matches(*argv, "help") == 0) {
 167                dcb_maxrate_help();
 168                return 0;
 169        } else if (matches(*argv, "show") == 0) {
 170                NEXT_ARG_FWD();
 171                return dcb_cmd_parse_dev(dcb, argc, argv,
 172                                         dcb_cmd_maxrate_show, dcb_maxrate_help_show);
 173        } else if (matches(*argv, "set") == 0) {
 174                NEXT_ARG_FWD();
 175                return dcb_cmd_parse_dev(dcb, argc, argv,
 176                                         dcb_cmd_maxrate_set, dcb_maxrate_help_set);
 177        } else {
 178                fprintf(stderr, "What is \"%s\"?\n", *argv);
 179                dcb_maxrate_help();
 180                return -EINVAL;
 181        }
 182}
 183