]> git.proxmox.com Git - mirror_frr.git/blob - lib/tc.c
doc: Add `show ipv6 rpf X:X::X:X` command to docs
[mirror_frr.git] / lib / tc.c
1 /*
2 * Traffic Control (TC) main library
3 * Copyright (C) 2022 Shichu Yang
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "tc.h"
21
22 int tc_getrate(const char *str, uint64_t *rate)
23 {
24 char *endp;
25 uint64_t raw = strtoull(str, &endp, 10);
26
27 if (endp == str)
28 return -1;
29
30 /* if the string only contains a number, it must be valid rate (bps) */
31 bool valid = (*endp == '\0');
32
33 const char *p = endp;
34 bool bytes = false, binary_base = false;
35 int power = 0;
36
37 while (*p) {
38 if (strcmp(p, "Bps") == 0) {
39 bytes = true;
40 valid = true;
41 break;
42 } else if (strcmp(p, "bit") == 0) {
43 valid = true;
44 break;
45 }
46 switch (*p) {
47 case 'k':
48 case 'K':
49 power = 1;
50 break;
51 case 'm':
52 case 'M':
53 power = 2;
54 break;
55 case 'g':
56 case 'G':
57 power = 3;
58 break;
59 case 't':
60 case 'T':
61 power = 4;
62 break;
63 case 'i':
64 case 'I':
65 if (power != 0)
66 binary_base = true;
67 else
68 return -1;
69 break;
70 default:
71 return -1;
72 }
73 p++;
74 }
75
76 if (!valid)
77 return -1;
78
79 for (int i = 0; i < power; i++)
80 raw *= binary_base ? 1024ULL : 1000ULL;
81
82 if (bytes)
83 *rate = raw;
84 else
85 *rate = raw / 8ULL;
86
87 return 0;
88 }