]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_rlim.c
Add missing prefix bit length for addrlabel
[mirror_iproute2.git] / tc / q_rlim.c
1 /*
2 * q_rtlim.c RTLIM.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <syslog.h>
17 #include <fcntl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <string.h>
22
23 #include "utils.h"
24 #include "tc_util.h"
25
26 static void explain(void)
27 {
28 fprintf(stderr,
29 "Usage: ... rlim limit PACKETS rate KBPS [ overhead BYTES ]\n");
30 }
31
32 static void explain1(char *arg)
33 {
34 fprintf(stderr, "Illegal \"%s\"\n", arg);
35 }
36
37
38 #define usage() return(-1)
39
40 static int rlim_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
41 {
42 unsigned x;
43 struct tc_rlim_qopt opt = {
44 .overhead = 24, /* Ether IPG + Preamble + CRC */
45 };
46 struct rtattr *tail;
47
48 while (argc > 0) {
49 if (matches(*argv, "limit") == 0) {
50 NEXT_ARG();
51 if (opt.limit) {
52 fprintf(stderr, "Double \"limit\" spec\n");
53 return -1;
54 }
55 if (get_size(&opt.limit, *argv)) {
56 explain1("limit");
57 return -1;
58 }
59 } else if (strcmp(*argv, "rate") == 0) {
60 NEXT_ARG();
61 if (opt.rate) {
62 fprintf(stderr, "Double \"rate\" spec\n");
63 return -1;
64 }
65
66 if (get_rate(&x, *argv)) {
67 explain1("rate");
68 return -1;
69 }
70 opt.rate = x;
71 } else if (strcmp(*argv, "help") == 0) {
72 explain();
73 return -1;
74 } else {
75 fprintf(stderr, "What is \"%s\"?\n", *argv);
76 explain();
77 return -1;
78 }
79 argc--; argv++;
80 }
81
82 if (opt.rate == 0) {
83 fprintf(stderr, "\"rate\" is required.\n");
84 return -1;
85 }
86
87 if (opt.limit == 0)
88 opt.limit = 1000;
89
90 tail = NLMSG_TAIL(n);
91 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
92 addattr_l(n, 2024, TCA_RLIM_PARMS, &opt, sizeof(opt));
93 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
94
95 return 0;
96 }
97
98 static int rlim_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
99 {
100 struct rtattr *tb[TCA_RLIM_PARMS+1];
101 struct tc_rlim_qopt *qopt;
102 SPRINT_BUF(b1);
103 SPRINT_BUF(b2);
104
105 if (opt == NULL)
106 return 0;
107
108 parse_rtattr_nested(tb, TCA_RLIM_PARMS, opt);
109 if (tb[TCA_RLIM_PARMS] == NULL)
110 return -1;
111
112 qopt = RTA_DATA(tb[TCA_RLIM_PARMS]);
113 if (RTA_PAYLOAD(tb[TCA_RLIM_PARMS]) < sizeof(*qopt))
114 return -1;
115
116 fprintf(f, "limit %s rate %s overhead %u",
117 sprint_size(qopt->limit, b1),
118 sprint_rate(qopt->rate, b2),
119 qopt->overhead);
120
121 return 0;
122 }
123
124 struct qdisc_util rlim_qdisc_util = {
125 .id = "rlim",
126 .parse_qopt = rlim_parse_opt,
127 .print_qopt = rlim_print_opt,
128 };
129