]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_gact.c
Merge branch 'master' into net-next
[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 | pipe |\n"
49 " \t goto chain <CHAIN_INDEX>\n"
50 "\tRAND := random <RANDTYPE> <ACTION> <VAL>\n"
51 "\tRANDTYPE := netrand | determ\n"
52 "\tVAL : = value not exceeding 10000\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>\n"
60 "\tINDEX := index value used\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 int ok = 0;
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
91 if (matches(*argv, "gact") == 0) {
92 ok++;
93 } else {
94 if (parse_action_control(&argc, &argv, &p.action, false) == -1)
95 usage();
96 ok++;
97 }
98
99 if (ok) {
100 argc--;
101 argv++;
102 }
103
104 #ifdef CONFIG_GACT_PROB
105 if (ok && argc > 0) {
106 if (matches(*argv, "random") == 0) {
107 rd = 1;
108 NEXT_ARG();
109 if (matches(*argv, "netrand") == 0) {
110 NEXT_ARG();
111 pp.ptype = PGACT_NETRAND;
112 } else if (matches(*argv, "determ") == 0) {
113 NEXT_ARG();
114 pp.ptype = PGACT_DETERM;
115 } else {
116 fprintf(stderr, "Illegal \"random type\"\n");
117 return -1;
118 }
119
120 if (parse_action_control(&argc, &argv,
121 &pp.paction, false) == -1)
122 usage();
123 argc--;
124 argv++;
125 if (get_u16(&pp.pval, *argv, 10)) {
126 fprintf(stderr, "Illegal probability val 0x%x\n", pp.pval);
127 return -1;
128 }
129 if (pp.pval > 10000) {
130 fprintf(stderr, "Illegal probability val 0x%x\n", pp.pval);
131 return -1;
132 }
133 argc--;
134 argv++;
135 } else if (matches(*argv, "help") == 0) {
136 usage();
137 }
138 }
139 #endif
140
141 if (argc > 0) {
142 if (matches(*argv, "index") == 0) {
143 NEXT_ARG();
144 if (get_u32(&p.index, *argv, 10)) {
145 fprintf(stderr, "Illegal \"index\"\n");
146 return -1;
147 }
148 argc--;
149 argv++;
150 ok++;
151 } else if (matches(*argv, "help") == 0) {
152 usage();
153 }
154 }
155
156 if (!ok)
157 return -1;
158
159 tail = NLMSG_TAIL(n);
160 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
161 addattr_l(n, MAX_MSG, TCA_GACT_PARMS, &p, sizeof(p));
162 #ifdef CONFIG_GACT_PROB
163 if (rd) {
164 addattr_l(n, MAX_MSG, TCA_GACT_PROB, &pp, sizeof(pp));
165 }
166 #endif
167 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
168
169 *argc_p = argc;
170 *argv_p = argv;
171 return 0;
172 }
173
174 static int
175 print_gact(struct action_util *au, FILE * f, struct rtattr *arg)
176 {
177 #ifdef CONFIG_GACT_PROB
178 struct tc_gact_p *pp = NULL;
179 struct tc_gact_p pp_dummy;
180 #endif
181 struct tc_gact *p = NULL;
182 struct rtattr *tb[TCA_GACT_MAX + 1];
183
184 if (arg == NULL)
185 return -1;
186
187 parse_rtattr_nested(tb, TCA_GACT_MAX, arg);
188
189 if (tb[TCA_GACT_PARMS] == NULL) {
190 fprintf(f, "[NULL gact parameters]");
191 return -1;
192 }
193 p = RTA_DATA(tb[TCA_GACT_PARMS]);
194
195 fprintf(f, "gact ");
196 print_action_control(f, "action ", p->action, "");
197 #ifdef CONFIG_GACT_PROB
198 if (tb[TCA_GACT_PROB] != NULL) {
199 pp = RTA_DATA(tb[TCA_GACT_PROB]);
200 } else {
201 /* need to keep consistent output */
202 memset(&pp_dummy, 0, sizeof(pp_dummy));
203 pp = &pp_dummy;
204 }
205 fprintf(f, "\n\t random type %s", prob_n2a(pp->ptype));
206 print_action_control(f, " ", pp->paction, " ");
207 fprintf(f, "val %d", pp->pval);
208 #endif
209 fprintf(f, "\n\t index %u ref %d bind %d", p->index, p->refcnt,
210 p->bindcnt);
211 if (show_stats) {
212 if (tb[TCA_GACT_TM]) {
213 struct tcf_t *tm = RTA_DATA(tb[TCA_GACT_TM]);
214
215 print_tm(f, tm);
216 }
217 }
218 fprintf(f, "\n ");
219 return 0;
220 }
221
222 struct action_util gact_action_util = {
223 .id = "gact",
224 .parse_aopt = parse_gact,
225 .print_aopt = print_gact,
226 };