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