1
2
3
4
5
6
7
8
9
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
27
28#ifdef CONFIG_GACT_PROB
29static 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
41static void
42explain(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 "Where: \tACTION := reclassify | drop | continue | pass | pipe |\n"
58 " \t goto chain <CHAIN_INDEX> | jump <JUMP_COUNT>\n"
59 "\tINDEX := index value used\n"
60 "\tJUMP_COUNT := Absolute jump from start of action list\n"
61 "\n");
62#endif
63}
64
65
66static void
67usage(void)
68{
69 explain();
70 exit(-1);
71}
72
73static int
74parse_gact(struct action_util *a, int *argc_p, char ***argv_p,
75 int tca_id, struct nlmsghdr *n)
76{
77 int argc = *argc_p;
78 char **argv = *argv_p;
79 struct tc_gact p = { 0 };
80#ifdef CONFIG_GACT_PROB
81 int rd = 0;
82 struct tc_gact_p pp;
83#endif
84 struct rtattr *tail;
85
86 if (argc < 0)
87 return -1;
88
89 if (!matches(*argv, "gact"))
90 NEXT_ARG();
91
92 if (!matches(*argv, "index"))
93 goto skip_args;
94 if (parse_action_control(&argc, &argv, &p.action, false))
95 usage();
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 if (get_u16(&pp.pval, *argv, 10)) {
117 fprintf(stderr,
118 "Illegal probability val 0x%x\n",
119 pp.pval);
120 return -1;
121 }
122 if (pp.pval > 10000) {
123 fprintf(stderr,
124 "Illegal probability val 0x%x\n",
125 pp.pval);
126 return -1;
127 }
128 argc--;
129 argv++;
130 } else if (matches(*argv, "help") == 0) {
131 usage();
132 }
133 }
134#endif
135
136 if (argc > 0) {
137 if (matches(*argv, "index") == 0) {
138skip_args:
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 = addattr_nest(n, MAX_MSG, tca_id);
152 addattr_l(n, MAX_MSG, TCA_GACT_PARMS, &p, sizeof(p));
153#ifdef CONFIG_GACT_PROB
154 if (rd)
155 addattr_l(n, MAX_MSG, TCA_GACT_PROB, &pp, sizeof(pp));
156#endif
157 addattr_nest_end(n, tail);
158
159 *argc_p = argc;
160 *argv_p = argv;
161 return 0;
162}
163
164static int
165print_gact(struct action_util *au, FILE *f, struct rtattr *arg)
166{
167#ifdef CONFIG_GACT_PROB
168 struct tc_gact_p *pp = NULL;
169 struct tc_gact_p pp_dummy;
170#endif
171 struct tc_gact *p = NULL;
172 struct rtattr *tb[TCA_GACT_MAX + 1];
173
174 print_string(PRINT_ANY, "kind", "%s ", "gact");
175 if (arg == NULL)
176 return 0;
177
178 parse_rtattr_nested(tb, TCA_GACT_MAX, arg);
179
180 if (tb[TCA_GACT_PARMS] == NULL) {
181 fprintf(stderr, "Missing gact parameters\n");
182 return -1;
183 }
184 p = RTA_DATA(tb[TCA_GACT_PARMS]);
185
186 print_action_control(f, "action ", p->action, "");
187#ifdef CONFIG_GACT_PROB
188 if (tb[TCA_GACT_PROB] != NULL) {
189 pp = RTA_DATA(tb[TCA_GACT_PROB]);
190 } else {
191
192 memset(&pp_dummy, 0, sizeof(pp_dummy));
193 pp = &pp_dummy;
194 }
195 open_json_object("prob");
196 print_nl();
197 print_string(PRINT_ANY, "random_type", "\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_nl();
204 print_uint(PRINT_ANY, "index", "\t index %u", p->index);
205 print_int(PRINT_ANY, "ref", " ref %d", p->refcnt);
206 print_int(PRINT_ANY, "bind", " bind %d", p->bindcnt);
207 if (show_stats) {
208 if (tb[TCA_GACT_TM]) {
209 struct tcf_t *tm = RTA_DATA(tb[TCA_GACT_TM]);
210
211 print_tm(f, tm);
212 }
213 }
214 print_nl();
215 return 0;
216}
217
218struct action_util gact_action_util = {
219 .id = "gact",
220 .parse_aopt = parse_gact,
221 .print_aopt = print_gact,
222};
223