blob: 9f07851a6dab9e9b03bd4f43f27f9397472c29cf [file] [log] [blame]
osdl.net!shemminger2265da02004-08-30 21:20:10 +00001/*
Stephen Hemmingerae665a52006-12-05 10:10:22 -08002 * m_gact.c generic actions module
osdl.net!shemminger2265da02004-08-30 21:20:10 +00003 *
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 *
Stephen Hemmingerae665a52006-12-05 10:10:22 -08009 * Authors: J Hadi Salim (hadi@cyberus.ca)
10 *
osdl.net!shemminger2265da02004-08-30 21:20:10 +000011 */
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
Stephen Hemmingerae665a52006-12-05 10:10:22 -080030static const char *prob_n2a(int p)
osdl.net!shemminger2265da02004-08-30 21:20:10 +000031{
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,
Jamal Hadi Salimebf32082006-08-08 12:10:08 -070048 "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"
Stephen Hemminger302d3fb2006-09-25 17:08:40 -070053 "\n");
osdl.net!shemminger2265da02004-08-30 21:20:10 +000054#else
55 fprintf(stderr, "Usage: ... gact <ACTION> [INDEX]\n");
56 fprintf(stderr,
Stephen Hemminger302d3fb2006-09-25 17:08:40 -070057 "Where: \tACTION := reclassify | drop | continue | pass \n"
Jamal Hadi Salimebf32082006-08-08 12:10:08 -070058 "\tINDEX := index value used\n"
jamalf1e4f042006-08-04 10:59:34 -070059 "\n");
osdl.net!shemminger2265da02004-08-30 21:20:10 +000060#endif
61}
62
Stephen Hemminger302d3fb2006-09-25 17:08:40 -070063
Jamal Hadi Salimebf32082006-08-08 12:10:08 -070064static void
65usage(void)
66{
67 explain();
68 exit(-1);
69}
70
osdl.net!shemminger2265da02004-08-30 21:20:10 +000071int
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 {
87 fprintf(stderr,"bad action type %s\n",*argv);
88 return -10;
89 }
90}
91
92int
93parse_gact(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
94{
95 int argc = *argc_p;
96 char **argv = *argv_p;
97 int ok = 0;
98 int action = TC_POLICE_RECLASSIFY;
99 struct tc_gact p;
100#ifdef CONFIG_GACT_PROB
101 int rd = 0;
102 struct tc_gact_p pp;
103#endif
104 struct rtattr *tail;
105
106 memset(&p, 0, sizeof (p));
107 p.action = TC_POLICE_RECLASSIFY;
108
109 if (argc < 0)
110 return -1;
111
112
113 if (matches(*argv, "gact") == 0) {
114 ok++;
115 } else {
116 action = get_act(&argv);
117 if (action != -10) {
118 p.action = action;
119 ok++;
120 } else {
121 explain();
122 return action;
123 }
124 }
125
126 if (ok) {
127 argc--;
128 argv++;
129 }
130
131#ifdef CONFIG_GACT_PROB
132 if (ok && argc > 0) {
133 if (matches(*argv, "random") == 0) {
134 rd = 1;
135 NEXT_ARG();
136 if (matches(*argv, "netrand") == 0) {
137 NEXT_ARG();
138 pp.ptype = PGACT_NETRAND;
139 } else if (matches(*argv, "determ") == 0) {
140 NEXT_ARG();
141 pp.ptype = PGACT_DETERM;
142 } else {
143 fprintf(stderr, "Illegal \"random type\"\n");
144 return -1;
145 }
146
147 action = get_act(&argv);
148 if (action != -10) { /* FIXME */
149 pp.paction = action;
150 } else {
151 explain();
152 return -1;
153 }
154 argc--;
155 argv++;
156 if (get_u16(&pp.pval, *argv, 10)) {
157 fprintf(stderr, "Illegal probability val 0x%x\n",pp.pval);
158 return -1;
159 }
160 if (pp.pval > 10000) {
161 fprintf(stderr, "Illegal probability val 0x%x\n",pp.pval);
162 return -1;
163 }
164 argc--;
165 argv++;
Jamal Hadi Salimebf32082006-08-08 12:10:08 -0700166 } else if (matches(*argv, "help") == 0) {
167 usage();
osdl.net!shemminger2265da02004-08-30 21:20:10 +0000168 }
169 }
170#endif
171
172 if (argc > 0) {
173 if (matches(*argv, "index") == 0) {
174 NEXT_ARG();
175 if (get_u32(&p.index, *argv, 10)) {
176 fprintf(stderr, "Illegal \"index\"\n");
177 return -1;
178 }
179 argc--;
180 argv++;
181 ok++;
jamalf1e4f042006-08-04 10:59:34 -0700182 } else if (matches(*argv, "help") == 0) {
Stephen Hemminger302d3fb2006-09-25 17:08:40 -0700183 usage();
osdl.net!shemminger2265da02004-08-30 21:20:10 +0000184 }
185 }
186
187 if (!ok)
188 return -1;
189
4!tgraf228569c2005-01-18 01:24:18 +0000190 tail = NLMSG_TAIL(n);
osdl.net!shemminger2265da02004-08-30 21:20:10 +0000191 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
192 addattr_l(n, MAX_MSG, TCA_GACT_PARMS, &p, sizeof (p));
193#ifdef CONFIG_GACT_PROB
194 if (rd) {
195 addattr_l(n, MAX_MSG, TCA_GACT_PROB, &pp, sizeof (pp));
196 }
197#endif
4!tgraf228569c2005-01-18 01:24:18 +0000198 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
osdl.net!shemminger2265da02004-08-30 21:20:10 +0000199
200 *argc_p = argc;
201 *argv_p = argv;
202 return 0;
203}
204
205int
206print_gact(struct action_util *au,FILE * f, struct rtattr *arg)
207{
208 SPRINT_BUF(b1);
209#ifdef CONFIG_GACT_PROB
210 SPRINT_BUF(b2);
211 struct tc_gact_p *pp = NULL;
212 struct tc_gact_p pp_dummy;
213#endif
214 struct tc_gact *p = NULL;
215 struct rtattr *tb[TCA_GACT_MAX + 1];
216
217 if (arg == NULL)
218 return -1;
219
4!tgraf3b3ecd32005-01-18 22:11:58 +0000220 parse_rtattr_nested(tb, TCA_GACT_MAX, arg);
osdl.net!shemminger2265da02004-08-30 21:20:10 +0000221
222 if (tb[TCA_GACT_PARMS] == NULL) {
223 fprintf(f, "[NULL gact parameters]");
224 return -1;
225 }
226 p = RTA_DATA(tb[TCA_GACT_PARMS]);
227
228 fprintf(f, "gact action %s", action_n2a(p->action, b1, sizeof (b1)));
229#ifdef CONFIG_GACT_PROB
230 if (NULL != tb[TCA_GACT_PROB]) {
231 pp = RTA_DATA(tb[TCA_GACT_PROB]);
232 } else {
233 /* need to keep consistent output */
234 memset(&pp_dummy, 0, sizeof (pp_dummy));
235 pp = &pp_dummy;
236 }
237 fprintf(f, "\n\t random type %s %s val %d",prob_n2a(pp->ptype), action_n2a(pp->paction, b2, sizeof (b2)), pp->pval);
238#endif
239 fprintf(f, "\n\t index %d ref %d bind %d",p->index, p->refcnt, p->bindcnt);
240 if (show_stats) {
241 if (tb[TCA_GACT_TM]) {
242 struct tcf_t *tm = RTA_DATA(tb[TCA_GACT_TM]);
243 print_tm(f,tm);
244 }
245 }
246 fprintf(f, "\n ");
247 return 0;
248}
249
net[shemminger]!kaber95812b52004-09-28 18:35:49 +0000250struct action_util gact_action_util = {
osdl.net!shemminger2265da02004-08-30 21:20:10 +0000251 .id = "gact",
252 .parse_aopt = parse_gact,
253 .print_aopt = print_gact,
osdl.net!shemminger2265da02004-08-30 21:20:10 +0000254};