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