]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_gact.c
tc: util: Don't call NEXT_ARG_FWD() in __parse_action_control()
[mirror_iproute2.git] / tc / m_gact.c
1 /*
2 * m_gact.c generic actions module
3 *
4 * This program is free software; you can distribute 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: J Hadi Salim (hadi@cyberus.ca)
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <string.h>
21
22 #include "utils.h"
23 #include "tc_util.h"
24 #include <linux/tc_act/tc_gact.h>
25
26 /* define to turn on probablity stuff */
27
28 #ifdef CONFIG_GACT_PROB
29 static const char *prob_n2a(int p)
30 {
31 if (p == PGACT_NONE)
32 return "none";
33 if (p == PGACT_NETRAND)
34 return "netrand";
35 if (p == PGACT_DETERM)
36 return "determ";
37 return "none";
38 }
39 #endif
40
41 static void
42 explain(void)
43 {
44 #ifdef CONFIG_GACT_PROB
45 fprintf(stderr, "Usage: ... gact <ACTION> [RAND] [INDEX]\n");
46 fprintf(stderr,
47 "Where: \tACTION := reclassify | drop | continue | pass | pipe |\n"
48 " \t goto chain <CHAIN_INDEX> | jump <JUMP_COUNT>\n"
49 "\tRAND := random <RANDTYPE> <ACTION> <VAL>\n"
50 "\tRANDTYPE := netrand | determ\n"
51 "\tVAL : = value not exceeding 10000\n"
52 "\tJUMP_COUNT := Absolute jump from start of action list\n"
53 "\tINDEX := index value used\n"
54 "\n");
55 #else
56 fprintf(stderr, "Usage: ... gact <ACTION> [INDEX]\n");
57 fprintf(stderr,
58 "Where: \tACTION := reclassify | drop | continue | pass | pipe |\n"
59 " \t goto chain <CHAIN_INDEX> | jump <JUMP_COUNT>\n"
60 "\tINDEX := index value used\n"
61 "\tJUMP_COUNT := Absolute jump from start of action list\n"
62 "\n");
63 #endif
64 }
65
66
67 static void
68 usage(void)
69 {
70 explain();
71 exit(-1);
72 }
73
74 static int
75 parse_gact(struct action_util *a, int *argc_p, char ***argv_p,
76 int tca_id, struct nlmsghdr *n)
77 {
78 int argc = *argc_p;
79 char **argv = *argv_p;
80 struct tc_gact p = { 0 };
81 #ifdef CONFIG_GACT_PROB
82 int rd = 0;
83 struct tc_gact_p pp;
84 #endif
85 struct rtattr *tail;
86
87 if (argc < 0)
88 return -1;
89
90 if (matches(*argv, "gact") != 0 &&
91 parse_action_control(&argc, &argv, &p.action, false) == -1) {
92 usage(); /* does not return */
93 }
94
95 NEXT_ARG_FWD();
96
97 #ifdef CONFIG_GACT_PROB
98 if (argc > 0) {
99 if (matches(*argv, "random") == 0) {
100 rd = 1;
101 NEXT_ARG();
102 if (matches(*argv, "netrand") == 0) {
103 NEXT_ARG();
104 pp.ptype = PGACT_NETRAND;
105 } else if (matches(*argv, "determ") == 0) {
106 NEXT_ARG();
107 pp.ptype = PGACT_DETERM;
108 } else {
109 fprintf(stderr, "Illegal \"random type\"\n");
110 return -1;
111 }
112
113 if (parse_action_control(&argc, &argv,
114 &pp.paction, false) == -1)
115 usage();
116 NEXT_ARG_FWD();
117 if (get_u16(&pp.pval, *argv, 10)) {
118 fprintf(stderr,
119 "Illegal probability val 0x%x\n",
120 pp.pval);
121 return -1;
122 }
123 if (pp.pval > 10000) {
124 fprintf(stderr,
125 "Illegal probability val 0x%x\n",
126 pp.pval);
127 return -1;
128 }
129 argc--;
130 argv++;
131 } else if (matches(*argv, "help") == 0) {
132 usage();
133 }
134 }
135 #endif
136
137 if (argc > 0) {
138 if (matches(*argv, "index") == 0) {
139 NEXT_ARG();
140 if (get_u32(&p.index, *argv, 10)) {
141 fprintf(stderr, "Illegal \"index\"\n");
142 return -1;
143 }
144 argc--;
145 argv++;
146 } else if (matches(*argv, "help") == 0) {
147 usage();
148 }
149 }
150
151 tail = NLMSG_TAIL(n);
152 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
153 addattr_l(n, MAX_MSG, TCA_GACT_PARMS, &p, sizeof(p));
154 #ifdef CONFIG_GACT_PROB
155 if (rd)
156 addattr_l(n, MAX_MSG, TCA_GACT_PROB, &pp, sizeof(pp));
157 #endif
158 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
159
160 *argc_p = argc;
161 *argv_p = argv;
162 return 0;
163 }
164
165 static int
166 print_gact(struct action_util *au, FILE *f, struct rtattr *arg)
167 {
168 #ifdef CONFIG_GACT_PROB
169 struct tc_gact_p *pp = NULL;
170 struct tc_gact_p pp_dummy;
171 #endif
172 struct tc_gact *p = NULL;
173 struct rtattr *tb[TCA_GACT_MAX + 1];
174
175 if (arg == NULL)
176 return -1;
177
178 parse_rtattr_nested(tb, TCA_GACT_MAX, arg);
179
180 if (tb[TCA_GACT_PARMS] == NULL) {
181 print_string(PRINT_FP, NULL, "%s", "[NULL gact parameters]");
182 return -1;
183 }
184 p = RTA_DATA(tb[TCA_GACT_PARMS]);
185
186 print_string(PRINT_ANY, "kind", "%s ", "gact");
187 print_action_control(f, "action ", p->action, "");
188 #ifdef CONFIG_GACT_PROB
189 if (tb[TCA_GACT_PROB] != NULL) {
190 pp = RTA_DATA(tb[TCA_GACT_PROB]);
191 } else {
192 /* need to keep consistent output */
193 memset(&pp_dummy, 0, sizeof(pp_dummy));
194 pp = &pp_dummy;
195 }
196 open_json_object("prob");
197 print_string(PRINT_ANY, "random_type", "\n\t random type %s",
198 prob_n2a(pp->ptype));
199 print_action_control(f, " ", pp->paction, " ");
200 print_int(PRINT_ANY, "val", "val %d", pp->pval);
201 close_json_object();
202 #endif
203 print_uint(PRINT_ANY, "index", "\n\t index %u", p->index);
204 print_int(PRINT_ANY, "ref", " ref %d", p->refcnt);
205 print_int(PRINT_ANY, "bind", " bind %d", p->bindcnt);
206 if (show_stats) {
207 if (tb[TCA_GACT_TM]) {
208 struct tcf_t *tm = RTA_DATA(tb[TCA_GACT_TM]);
209
210 print_tm(f, tm);
211 }
212 }
213 print_string(PRINT_FP, NULL, "%s", "\n");
214 return 0;
215 }
216
217 struct action_util gact_action_util = {
218 .id = "gact",
219 .parse_aopt = parse_gact,
220 .print_aopt = print_gact,
221 };