]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_gact.c
rdma: Properly mark RDMAtool license
[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 fprintf(stderr,
58 "Where: \tACTION := reclassify | drop | continue | pass | pipe |\n"
59 " \t goto chain <CHAIN_INDEX> | jump <JUMP_COUNT>\n"
60 "\tINDEX := index value used\n"
61 "\tJUMP_COUNT := Absolute jump from start of action list\n"
62 "\n");
63 #endif
64 }
65
66
67 static void
68 usage(void)
69 {
70 explain();
71 exit(-1);
72 }
73
74 static int
75 parse_gact(struct action_util *a, int *argc_p, char ***argv_p,
76 int tca_id, struct nlmsghdr *n)
77 {
78 int argc = *argc_p;
79 char **argv = *argv_p;
80 struct tc_gact p = { 0 };
81 #ifdef CONFIG_GACT_PROB
82 int rd = 0;
83 struct tc_gact_p pp;
84 #endif
85 struct rtattr *tail;
86
87 if (argc < 0)
88 return -1;
89
90 if (!matches(*argv, "gact"))
91 NEXT_ARG_FWD();
92 if (parse_action_control(&argc, &argv, &p.action, false))
93 usage(); /* does not return */
94
95 #ifdef CONFIG_GACT_PROB
96 if (argc > 0) {
97 if (matches(*argv, "random") == 0) {
98 rd = 1;
99 NEXT_ARG();
100 if (matches(*argv, "netrand") == 0) {
101 NEXT_ARG();
102 pp.ptype = PGACT_NETRAND;
103 } else if (matches(*argv, "determ") == 0) {
104 NEXT_ARG();
105 pp.ptype = PGACT_DETERM;
106 } else {
107 fprintf(stderr, "Illegal \"random type\"\n");
108 return -1;
109 }
110
111 if (parse_action_control(&argc, &argv,
112 &pp.paction, false) == -1)
113 usage();
114 if (get_u16(&pp.pval, *argv, 10)) {
115 fprintf(stderr,
116 "Illegal probability val 0x%x\n",
117 pp.pval);
118 return -1;
119 }
120 if (pp.pval > 10000) {
121 fprintf(stderr,
122 "Illegal probability val 0x%x\n",
123 pp.pval);
124 return -1;
125 }
126 argc--;
127 argv++;
128 } else if (matches(*argv, "help") == 0) {
129 usage();
130 }
131 }
132 #endif
133
134 if (argc > 0) {
135 if (matches(*argv, "index") == 0) {
136 NEXT_ARG();
137 if (get_u32(&p.index, *argv, 10)) {
138 fprintf(stderr, "Illegal \"index\"\n");
139 return -1;
140 }
141 argc--;
142 argv++;
143 } else if (matches(*argv, "help") == 0) {
144 usage();
145 }
146 }
147
148 tail = addattr_nest(n, MAX_MSG, tca_id);
149 addattr_l(n, MAX_MSG, TCA_GACT_PARMS, &p, sizeof(p));
150 #ifdef CONFIG_GACT_PROB
151 if (rd)
152 addattr_l(n, MAX_MSG, TCA_GACT_PROB, &pp, sizeof(pp));
153 #endif
154 addattr_nest_end(n, tail);
155
156 *argc_p = argc;
157 *argv_p = argv;
158 return 0;
159 }
160
161 static int
162 print_gact(struct action_util *au, FILE *f, struct rtattr *arg)
163 {
164 #ifdef CONFIG_GACT_PROB
165 struct tc_gact_p *pp = NULL;
166 struct tc_gact_p pp_dummy;
167 #endif
168 struct tc_gact *p = NULL;
169 struct rtattr *tb[TCA_GACT_MAX + 1];
170
171 if (arg == NULL)
172 return -1;
173
174 parse_rtattr_nested(tb, TCA_GACT_MAX, arg);
175
176 if (tb[TCA_GACT_PARMS] == NULL) {
177 print_string(PRINT_FP, NULL, "%s", "[NULL gact parameters]");
178 return -1;
179 }
180 p = RTA_DATA(tb[TCA_GACT_PARMS]);
181
182 print_string(PRINT_ANY, "kind", "%s ", "gact");
183 print_action_control(f, "action ", p->action, "");
184 #ifdef CONFIG_GACT_PROB
185 if (tb[TCA_GACT_PROB] != NULL) {
186 pp = RTA_DATA(tb[TCA_GACT_PROB]);
187 } else {
188 /* need to keep consistent output */
189 memset(&pp_dummy, 0, sizeof(pp_dummy));
190 pp = &pp_dummy;
191 }
192 open_json_object("prob");
193 print_string(PRINT_ANY, "random_type", "\n\t random type %s",
194 prob_n2a(pp->ptype));
195 print_action_control(f, " ", pp->paction, " ");
196 print_int(PRINT_ANY, "val", "val %d", pp->pval);
197 close_json_object();
198 #endif
199 print_uint(PRINT_ANY, "index", "\n\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(f, tm);
207 }
208 }
209 print_string(PRINT_FP, NULL, "%s", "\n");
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 };