]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_gact.c
treewide: refactor help messages
[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 <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 /* define to turn on probablity stuff */
27
28 #ifdef CONFIG_GACT_PROB
29 static 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
41 static void
42 explain(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
66 static void
67 usage(void)
68 {
69 explain();
70 exit(-1);
71 }
72
73 static int
74 parse_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_FWD();
91 if (parse_action_control(&argc, &argv, &p.action, false))
92 usage(); /* does not return */
93
94 #ifdef CONFIG_GACT_PROB
95 if (argc > 0) {
96 if (matches(*argv, "random") == 0) {
97 rd = 1;
98 NEXT_ARG();
99 if (matches(*argv, "netrand") == 0) {
100 NEXT_ARG();
101 pp.ptype = PGACT_NETRAND;
102 } else if (matches(*argv, "determ") == 0) {
103 NEXT_ARG();
104 pp.ptype = PGACT_DETERM;
105 } else {
106 fprintf(stderr, "Illegal \"random type\"\n");
107 return -1;
108 }
109
110 if (parse_action_control(&argc, &argv,
111 &pp.paction, false) == -1)
112 usage();
113 if (get_u16(&pp.pval, *argv, 10)) {
114 fprintf(stderr,
115 "Illegal probability val 0x%x\n",
116 pp.pval);
117 return -1;
118 }
119 if (pp.pval > 10000) {
120 fprintf(stderr,
121 "Illegal probability val 0x%x\n",
122 pp.pval);
123 return -1;
124 }
125 argc--;
126 argv++;
127 } else if (matches(*argv, "help") == 0) {
128 usage();
129 }
130 }
131 #endif
132
133 if (argc > 0) {
134 if (matches(*argv, "index") == 0) {
135 NEXT_ARG();
136 if (get_u32(&p.index, *argv, 10)) {
137 fprintf(stderr, "Illegal \"index\"\n");
138 return -1;
139 }
140 argc--;
141 argv++;
142 } else if (matches(*argv, "help") == 0) {
143 usage();
144 }
145 }
146
147 tail = addattr_nest(n, MAX_MSG, tca_id);
148 addattr_l(n, MAX_MSG, TCA_GACT_PARMS, &p, sizeof(p));
149 #ifdef CONFIG_GACT_PROB
150 if (rd)
151 addattr_l(n, MAX_MSG, TCA_GACT_PROB, &pp, sizeof(pp));
152 #endif
153 addattr_nest_end(n, tail);
154
155 *argc_p = argc;
156 *argv_p = argv;
157 return 0;
158 }
159
160 static int
161 print_gact(struct action_util *au, FILE *f, struct rtattr *arg)
162 {
163 #ifdef CONFIG_GACT_PROB
164 struct tc_gact_p *pp = NULL;
165 struct tc_gact_p pp_dummy;
166 #endif
167 struct tc_gact *p = NULL;
168 struct rtattr *tb[TCA_GACT_MAX + 1];
169
170 if (arg == NULL)
171 return -1;
172
173 parse_rtattr_nested(tb, TCA_GACT_MAX, arg);
174
175 if (tb[TCA_GACT_PARMS] == NULL) {
176 print_string(PRINT_FP, NULL, "%s", "[NULL gact parameters]");
177 return -1;
178 }
179 p = RTA_DATA(tb[TCA_GACT_PARMS]);
180
181 print_string(PRINT_ANY, "kind", "%s ", "gact");
182 print_action_control(f, "action ", p->action, "");
183 #ifdef CONFIG_GACT_PROB
184 if (tb[TCA_GACT_PROB] != NULL) {
185 pp = RTA_DATA(tb[TCA_GACT_PROB]);
186 } else {
187 /* need to keep consistent output */
188 memset(&pp_dummy, 0, sizeof(pp_dummy));
189 pp = &pp_dummy;
190 }
191 open_json_object("prob");
192 print_string(PRINT_ANY, "random_type", "\n\t random type %s",
193 prob_n2a(pp->ptype));
194 print_action_control(f, " ", pp->paction, " ");
195 print_int(PRINT_ANY, "val", "val %d", pp->pval);
196 close_json_object();
197 #endif
198 print_uint(PRINT_ANY, "index", "\n\t index %u", p->index);
199 print_int(PRINT_ANY, "ref", " ref %d", p->refcnt);
200 print_int(PRINT_ANY, "bind", " bind %d", p->bindcnt);
201 if (show_stats) {
202 if (tb[TCA_GACT_TM]) {
203 struct tcf_t *tm = RTA_DATA(tb[TCA_GACT_TM]);
204
205 print_tm(f, tm);
206 }
207 }
208 print_string(PRINT_FP, NULL, "%s", "\n");
209 return 0;
210 }
211
212 struct action_util gact_action_util = {
213 .id = "gact",
214 .parse_aopt = parse_gact,
215 .print_aopt = print_gact,
216 };