]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_gact.c
Use C99 style initializers 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 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
92 static int
93 parse_gact(struct action_util *a, int *argc_p, char ***argv_p,
94 int tca_id, struct nlmsghdr *n)
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 = { .action = TC_POLICE_RECLASSIFY };
101 #ifdef CONFIG_GACT_PROB
102 int rd = 0;
103 struct tc_gact_p pp;
104 #endif
105 struct rtattr *tail;
106
107 if (argc < 0)
108 return -1;
109
110
111 if (matches(*argv, "gact") == 0) {
112 ok++;
113 } else {
114 action = get_act(&argv);
115 if (action != -10) {
116 p.action = action;
117 ok++;
118 } else {
119 explain();
120 return action;
121 }
122 }
123
124 if (ok) {
125 argc--;
126 argv++;
127 }
128
129 #ifdef CONFIG_GACT_PROB
130 if (ok && argc > 0) {
131 if (matches(*argv, "random") == 0) {
132 rd = 1;
133 NEXT_ARG();
134 if (matches(*argv, "netrand") == 0) {
135 NEXT_ARG();
136 pp.ptype = PGACT_NETRAND;
137 } else if (matches(*argv, "determ") == 0) {
138 NEXT_ARG();
139 pp.ptype = PGACT_DETERM;
140 } else {
141 fprintf(stderr, "Illegal \"random type\"\n");
142 return -1;
143 }
144
145 action = get_act(&argv);
146 if (action != -10) { /* FIXME */
147 pp.paction = action;
148 } else {
149 explain();
150 return -1;
151 }
152 argc--;
153 argv++;
154 if (get_u16(&pp.pval, *argv, 10)) {
155 fprintf(stderr, "Illegal probability val 0x%x\n", pp.pval);
156 return -1;
157 }
158 if (pp.pval > 10000) {
159 fprintf(stderr, "Illegal probability val 0x%x\n", pp.pval);
160 return -1;
161 }
162 argc--;
163 argv++;
164 } else if (matches(*argv, "help") == 0) {
165 usage();
166 }
167 }
168 #endif
169
170 if (argc > 0) {
171 if (matches(*argv, "index") == 0) {
172 NEXT_ARG();
173 if (get_u32(&p.index, *argv, 10)) {
174 fprintf(stderr, "Illegal \"index\"\n");
175 return -1;
176 }
177 argc--;
178 argv++;
179 ok++;
180 } else if (matches(*argv, "help") == 0) {
181 usage();
182 }
183 }
184
185 if (!ok)
186 return -1;
187
188 tail = NLMSG_TAIL(n);
189 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
190 addattr_l(n, MAX_MSG, TCA_GACT_PARMS, &p, sizeof(p));
191 #ifdef CONFIG_GACT_PROB
192 if (rd) {
193 addattr_l(n, MAX_MSG, TCA_GACT_PROB, &pp, sizeof(pp));
194 }
195 #endif
196 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
197
198 *argc_p = argc;
199 *argv_p = argv;
200 return 0;
201 }
202
203 static int
204 print_gact(struct action_util *au, FILE * f, struct rtattr *arg)
205 {
206 SPRINT_BUF(b1);
207 #ifdef CONFIG_GACT_PROB
208 SPRINT_BUF(b2);
209 struct tc_gact_p *pp = NULL;
210 struct tc_gact_p pp_dummy;
211 #endif
212 struct tc_gact *p = NULL;
213 struct rtattr *tb[TCA_GACT_MAX + 1];
214
215 if (arg == NULL)
216 return -1;
217
218 parse_rtattr_nested(tb, TCA_GACT_MAX, arg);
219
220 if (tb[TCA_GACT_PARMS] == NULL) {
221 fprintf(f, "[NULL gact parameters]");
222 return -1;
223 }
224 p = RTA_DATA(tb[TCA_GACT_PARMS]);
225
226 fprintf(f, "gact action %s", action_n2a(p->action, b1, sizeof(b1)));
227 #ifdef CONFIG_GACT_PROB
228 if (tb[TCA_GACT_PROB] != NULL) {
229 pp = RTA_DATA(tb[TCA_GACT_PROB]);
230 } else {
231 /* need to keep consistent output */
232 memset(&pp_dummy, 0, sizeof(pp_dummy));
233 pp = &pp_dummy;
234 }
235 fprintf(f, "\n\t random type %s %s val %d", prob_n2a(pp->ptype), action_n2a(pp->paction, b2, sizeof (b2)), pp->pval);
236 #endif
237 fprintf(f, "\n\t index %d ref %d bind %d", p->index, p->refcnt, p->bindcnt);
238 if (show_stats) {
239 if (tb[TCA_GACT_TM]) {
240 struct tcf_t *tm = RTA_DATA(tb[TCA_GACT_TM]);
241
242 print_tm(f, tm);
243 }
244 }
245 fprintf(f, "\n ");
246 return 0;
247 }
248
249 struct action_util gact_action_util = {
250 .id = "gact",
251 .parse_aopt = parse_gact,
252 .print_aopt = print_gact,
253 };