]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_gact.c
tc: use action_a2n() everywhere
[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 <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 #include <linux/tc_act/tc_gact.h>
26
27 /* define to turn on probablity stuff */
28
29 #ifdef CONFIG_GACT_PROB
30 static const char *prob_n2a(int p)
31 {
32 if (p == PGACT_NONE)
33 return "none";
34 if (p == PGACT_NETRAND)
35 return "netrand";
36 if (p == PGACT_DETERM)
37 return "determ";
38 return "none";
39 }
40 #endif
41
42 static void
43 explain(void)
44 {
45 #ifdef CONFIG_GACT_PROB
46 fprintf(stderr, "Usage: ... gact <ACTION> [RAND] [INDEX]\n");
47 fprintf(stderr,
48 "Where: \tACTION := reclassify | drop | continue | pass\n"
49 "\tRAND := random <RANDTYPE> <ACTION> <VAL>\n"
50 "\tRANDTYPE := netrand | determ\n"
51 "\tVAL : = value not exceeding 10000\n"
52 "\tINDEX := index value used\n"
53 "\n");
54 #else
55 fprintf(stderr, "Usage: ... gact <ACTION> [INDEX]\n");
56 fprintf(stderr,
57 "Where: \tACTION := reclassify | drop | continue | pass\n"
58 "\tINDEX := index value used\n"
59 "\n");
60 #endif
61 }
62
63
64 static void
65 usage(void)
66 {
67 explain();
68 exit(-1);
69 }
70
71 static int
72 get_act(char ***argv_p)
73 {
74 int n;
75
76 if (!action_a2n(**argv_p, &n, false)) {
77 fprintf(stderr, "bad action type %s\n", **argv_p);
78 return -10;
79 }
80 return n;
81 }
82
83 static int
84 parse_gact(struct action_util *a, int *argc_p, char ***argv_p,
85 int tca_id, struct nlmsghdr *n)
86 {
87 int argc = *argc_p;
88 char **argv = *argv_p;
89 int ok = 0;
90 int action = TC_POLICE_RECLASSIFY;
91 struct tc_gact p = { .action = TC_POLICE_RECLASSIFY };
92 #ifdef CONFIG_GACT_PROB
93 int rd = 0;
94 struct tc_gact_p pp;
95 #endif
96 struct rtattr *tail;
97
98 if (argc < 0)
99 return -1;
100
101
102 if (matches(*argv, "gact") == 0) {
103 ok++;
104 } else {
105 action = get_act(&argv);
106 if (action != -10) {
107 p.action = action;
108 ok++;
109 } else {
110 explain();
111 return action;
112 }
113 }
114
115 if (ok) {
116 argc--;
117 argv++;
118 }
119
120 #ifdef CONFIG_GACT_PROB
121 if (ok && argc > 0) {
122 if (matches(*argv, "random") == 0) {
123 rd = 1;
124 NEXT_ARG();
125 if (matches(*argv, "netrand") == 0) {
126 NEXT_ARG();
127 pp.ptype = PGACT_NETRAND;
128 } else if (matches(*argv, "determ") == 0) {
129 NEXT_ARG();
130 pp.ptype = PGACT_DETERM;
131 } else {
132 fprintf(stderr, "Illegal \"random type\"\n");
133 return -1;
134 }
135
136 action = get_act(&argv);
137 if (action != -10) { /* FIXME */
138 pp.paction = action;
139 } else {
140 explain();
141 return -1;
142 }
143 argc--;
144 argv++;
145 if (get_u16(&pp.pval, *argv, 10)) {
146 fprintf(stderr, "Illegal probability val 0x%x\n", pp.pval);
147 return -1;
148 }
149 if (pp.pval > 10000) {
150 fprintf(stderr, "Illegal probability val 0x%x\n", pp.pval);
151 return -1;
152 }
153 argc--;
154 argv++;
155 } else if (matches(*argv, "help") == 0) {
156 usage();
157 }
158 }
159 #endif
160
161 if (argc > 0) {
162 if (matches(*argv, "index") == 0) {
163 NEXT_ARG();
164 if (get_u32(&p.index, *argv, 10)) {
165 fprintf(stderr, "Illegal \"index\"\n");
166 return -1;
167 }
168 argc--;
169 argv++;
170 ok++;
171 } else if (matches(*argv, "help") == 0) {
172 usage();
173 }
174 }
175
176 if (!ok)
177 return -1;
178
179 tail = NLMSG_TAIL(n);
180 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
181 addattr_l(n, MAX_MSG, TCA_GACT_PARMS, &p, sizeof(p));
182 #ifdef CONFIG_GACT_PROB
183 if (rd) {
184 addattr_l(n, MAX_MSG, TCA_GACT_PROB, &pp, sizeof(pp));
185 }
186 #endif
187 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
188
189 *argc_p = argc;
190 *argv_p = argv;
191 return 0;
192 }
193
194 static int
195 print_gact(struct action_util *au, FILE * f, struct rtattr *arg)
196 {
197 SPRINT_BUF(b1);
198 #ifdef CONFIG_GACT_PROB
199 SPRINT_BUF(b2);
200 struct tc_gact_p *pp = NULL;
201 struct tc_gact_p pp_dummy;
202 #endif
203 struct tc_gact *p = NULL;
204 struct rtattr *tb[TCA_GACT_MAX + 1];
205
206 if (arg == NULL)
207 return -1;
208
209 parse_rtattr_nested(tb, TCA_GACT_MAX, arg);
210
211 if (tb[TCA_GACT_PARMS] == NULL) {
212 fprintf(f, "[NULL gact parameters]");
213 return -1;
214 }
215 p = RTA_DATA(tb[TCA_GACT_PARMS]);
216
217 fprintf(f, "gact action %s", action_n2a(p->action, b1, sizeof(b1)));
218 #ifdef CONFIG_GACT_PROB
219 if (tb[TCA_GACT_PROB] != NULL) {
220 pp = RTA_DATA(tb[TCA_GACT_PROB]);
221 } else {
222 /* need to keep consistent output */
223 memset(&pp_dummy, 0, sizeof(pp_dummy));
224 pp = &pp_dummy;
225 }
226 fprintf(f, "\n\t random type %s %s val %d", prob_n2a(pp->ptype), action_n2a(pp->paction, b2, sizeof (b2)), pp->pval);
227 #endif
228 fprintf(f, "\n\t index %d ref %d bind %d", p->index, p->refcnt, p->bindcnt);
229 if (show_stats) {
230 if (tb[TCA_GACT_TM]) {
231 struct tcf_t *tm = RTA_DATA(tb[TCA_GACT_TM]);
232
233 print_tm(f, tm);
234 }
235 }
236 fprintf(f, "\n ");
237 return 0;
238 }
239
240 struct action_util gact_action_util = {
241 .id = "gact",
242 .parse_aopt = parse_gact,
243 .print_aopt = print_gact,
244 };