Debian Codegraph @

tc/m_gact.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * m_gact.c generic actions module
4 *
5 * Authors: J Hadi Salim ([email protected])
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <string.h>
16
17 #include "utils.h"
18 #include "tc_util.h"
19 #include <linux/tc_act/tc_gact.h>
20
21 /* define to turn on probability stuff */
22
23 #ifdef CONFIG_GACT_PROB
24 static const char *prob_n2a(int p)
25 {
26 if (p == PGACT_NONE)
27 return "none";
28 if (p == PGACT_NETRAND)
29 return "netrand";
30 if (p == PGACT_DETERM)
31 return "determ";
32 return "none";
33 }
34 #endif
35
36 static void
37 explain(void)
38 {
39 #ifdef CONFIG_GACT_PROB
40 fprintf(stderr, "Usage: ... gact <ACTION> [RAND] [INDEX]\n");
41 fprintf(stderr,
42 "Where: \tACTION := reclassify | drop | continue | pass | pipe |\n"
43 " \t goto chain <CHAIN_INDEX> | jump <JUMP_COUNT>\n"
44 "\tRAND := random <RANDTYPE> <ACTION> <VAL>\n"
45 "\tRANDTYPE := netrand | determ\n"
46 "\tVAL : = value not exceeding 10000\n"
47 "\tJUMP_COUNT := Absolute jump from start of action list\n"
48 "\tINDEX := index value used\n"
49 "\n");
50 #else
51 fprintf(stderr, "Usage: ... gact <ACTION> [INDEX]\n"
52 "Where: \tACTION := reclassify | drop | continue | pass | pipe |\n"
53 " \t goto chain <CHAIN_INDEX> | jump <JUMP_COUNT>\n"
54 "\tINDEX := index value used\n"
55 "\tJUMP_COUNT := Absolute jump from start of action list\n"
56 "\n");
57 #endif
58 }
59
60
61 static void
62 usage(void)
63 {
64 explain();
65 exit(-1);
66 }
67
68 static int
69 parse_gact(const struct action_util *a, int *argc_p, char ***argv_p,
70 int tca_id, struct nlmsghdr *n)
71 {
72 int argc = *argc_p;
73 char **argv = *argv_p;
74 struct tc_gact p = { 0 };
75 #ifdef CONFIG_GACT_PROB
76 int rd = 0;
77 struct tc_gact_p pp;
78 #endif
79 struct rtattr *tail;
80
81 if (argc < 0)
82 return -1;
83
84 if (!matches(*argv, "gact"))
85 NEXT_ARG();
86 /* we're binding existing gact action to filter by index. */
87 if (!matches(*argv, "index"))
88 goto skip_args;
89 if (parse_action_control(&argc, &argv, &p.action, false))
90 usage(); /* does not return */
91
92 #ifdef CONFIG_GACT_PROB
93 if (argc > 0) {
94 if (matches(*argv, "random") == 0) {
95 rd = 1;
96 NEXT_ARG();
97 if (matches(*argv, "netrand") == 0) {
98 NEXT_ARG();
99 pp.ptype = PGACT_NETRAND;
100 } else if (matches(*argv, "determ") == 0) {
101 NEXT_ARG();
102 pp.ptype = PGACT_DETERM;
103 } else {
104 fprintf(stderr, "Illegal \"random type\"\n");
105 return -1;
106 }
107
108 if (parse_action_control(&argc, &argv,
109 &pp.paction, false) == -1)
110 usage();
111 if (get_u16(&pp.pval, *argv, 10)) {
112 fprintf(stderr,
113 "Illegal probability val 0x%x\n",
114 pp.pval);
115 return -1;
116 }
117 if (pp.pval > 10000) {
118 fprintf(stderr,
119 "Illegal probability val 0x%x\n",
120 pp.pval);
121 return -1;
122 }
123 argc--;
124 argv++;
125 } else if (matches(*argv, "help") == 0) {
126 usage();
127 }
128 }
129 #endif
130
131 if (argc > 0) {
132 if (matches(*argv, "index") == 0) {
133 skip_args:
134 NEXT_ARG();
135 if (get_u32(&p.index, *argv, 10)) {
136 fprintf(stderr, "Illegal \"index\"\n");
137 return -1;
138 }
139 argc--;
140 argv++;
141 } else if (matches(*argv, "help") == 0) {
142 usage();
143 }
144 }
145
146 tail = addattr_nest(n, MAX_MSG, tca_id);
147 addattr_l(n, MAX_MSG, TCA_GACT_PARMS, &p, sizeof(p));
148 #ifdef CONFIG_GACT_PROB
149 if (rd)
150 addattr_l(n, MAX_MSG, TCA_GACT_PROB, &pp, sizeof(pp));
151 #endif
152 addattr_nest_end(n, tail);
153
154 *argc_p = argc;
155 *argv_p = argv;
156 return 0;
157 }
158
159 static int
160 print_gact(const struct action_util *au, FILE *f, struct rtattr *arg)
161 {
162 #ifdef CONFIG_GACT_PROB
163 struct tc_gact_p *pp = NULL;
164 struct tc_gact_p pp_dummy;
165 #endif
166 struct tc_gact *p = NULL;
167 struct rtattr *tb[TCA_GACT_MAX + 1];
168
169 print_string(PRINT_ANY, "kind", "%s ", "gact");
170 if (arg == NULL)
171 return 0;
172
173 parse_rtattr_nested(tb, TCA_GACT_MAX, arg);
174
175 if (tb[TCA_GACT_PARMS] == NULL) {
176 fprintf(stderr, "Missing gact parameters\n");
177 return -1;
178 }
179 p = RTA_DATA(tb[TCA_GACT_PARMS]);
180
181 print_action_control("action ", p->action, "");
182 #ifdef CONFIG_GACT_PROB
183 if (tb[TCA_GACT_PROB] != NULL) {
184 pp = RTA_DATA(tb[TCA_GACT_PROB]);
185 } else {
186 /* need to keep consistent output */
187 memset(&pp_dummy, 0, sizeof(pp_dummy));
188 pp = &pp_dummy;
189 }
190 open_json_object("prob");
191 print_nl();
192 print_string(PRINT_ANY, "random_type", "\t random type %s",
193 prob_n2a(pp->ptype));
194 print_action_control(" ", pp->paction, " ");
195 print_int(PRINT_ANY, "val", "val %d", pp->pval);
196 close_json_object();
197 #endif
198 print_nl();
199 print_uint(PRINT_ANY, "index", "\t index %u", p->index);
200 print_int(PRINT_ANY, "ref", " ref %d", p->refcnt);
201 print_int(PRINT_ANY, "bind", " bind %d", p->bindcnt);
202 if (show_stats) {
203 if (tb[TCA_GACT_TM]) {
204 struct tcf_t *tm = RTA_DATA(tb[TCA_GACT_TM]);
205
206 print_tm(tm);
207 }
208 }
209 print_nl();
210 return 0;
211 }
212
213 struct action_util gact_action_util = {
214 .id = "gact",
215 .parse_aopt = parse_gact,
216 .print_aopt = print_gact,
217 };