]> 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> | jump <JUMP_COUNT>\n"
50 "\tRAND := random <RANDTYPE> <ACTION> <VAL>\n"
51 "\tRANDTYPE := netrand | determ\n"
52 "\tVAL : = value not exceeding 10000\n"
53 "\tJUMP_COUNT := Absolute jump from start of action list\n"
54 "\tINDEX := index value used\n"
55 "\n");
56 #else
57 fprintf(stderr, "Usage: ... gact <ACTION> [INDEX]\n");
58 fprintf(stderr,
59 "Where: \tACTION := reclassify | drop | continue | pass | pipe |\n"
60 " \t goto chain <CHAIN_INDEX> | jump <JUMP_COUNT>\n"
61 "\tINDEX := index value used\n"
62 "\tJUMP_COUNT := Absolute jump from start of action list\n"
63 "\n");
64 #endif
65 }
66
67
68 static void
69 usage(void)
70 {
71 explain();
72 exit(-1);
73 }
74
75 static int
76 parse_gact(struct action_util *a, int *argc_p, char ***argv_p,
77 int tca_id, struct nlmsghdr *n)
78 {
79 int argc = *argc_p;
80 char **argv = *argv_p;
81 struct tc_gact p = { 0 };
82 #ifdef CONFIG_GACT_PROB
83 int rd = 0;
84 struct tc_gact_p pp;
85 #endif
86 struct rtattr *tail;
87
88 if (argc < 0)
89 return -1;
90
91
92 if (matches(*argv, "gact") == 0) {
93 argc--;
94 argv++;
95 } else if (parse_action_control(&argc, &argv, &p.action, false) == -1) {
96 usage(); /* does not return */
97 }
98
99 #ifdef CONFIG_GACT_PROB
100 if (argc > 0) {
101 if (matches(*argv, "random") == 0) {
102 rd = 1;
103 NEXT_ARG();
104 if (matches(*argv, "netrand") == 0) {
105 NEXT_ARG();
106 pp.ptype = PGACT_NETRAND;
107 } else if (matches(*argv, "determ") == 0) {
108 NEXT_ARG();
109 pp.ptype = PGACT_DETERM;
110 } else {
111 fprintf(stderr, "Illegal \"random type\"\n");
112 return -1;
113 }
114
115 if (parse_action_control(&argc, &argv,
116 &pp.paction, false) == -1)
117 usage();
118 if (get_u16(&pp.pval, *argv, 10)) {
119 fprintf(stderr, "Illegal probability val 0x%x\n", pp.pval);
120 return -1;
121 }
122 if (pp.pval > 10000) {
123 fprintf(stderr, "Illegal probability val 0x%x\n", 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 = NLMSG_TAIL(n);
149 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
150 addattr_l(n, MAX_MSG, TCA_GACT_PARMS, &p, sizeof(p));
151 #ifdef CONFIG_GACT_PROB
152 if (rd) {
153 addattr_l(n, MAX_MSG, TCA_GACT_PROB, &pp, sizeof(pp));
154 }
155 #endif
156 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
157
158 *argc_p = argc;
159 *argv_p = argv;
160 return 0;
161 }
162
163 static int
164 print_gact(struct action_util *au, FILE * f, struct rtattr *arg)
165 {
166 #ifdef CONFIG_GACT_PROB
167 struct tc_gact_p *pp = NULL;
168 struct tc_gact_p pp_dummy;
169 #endif
170 struct tc_gact *p = NULL;
171 struct rtattr *tb[TCA_GACT_MAX + 1];
172
173 if (arg == NULL)
174 return -1;
175
176 parse_rtattr_nested(tb, TCA_GACT_MAX, arg);
177
178 if (tb[TCA_GACT_PARMS] == NULL) {
179 fprintf(f, "[NULL gact parameters]");
180 return -1;
181 }
182 p = RTA_DATA(tb[TCA_GACT_PARMS]);
183
184 fprintf(f, "gact ");
185 print_action_control(f, "action ", p->action, "");
186 #ifdef CONFIG_GACT_PROB
187 if (tb[TCA_GACT_PROB] != NULL) {
188 pp = RTA_DATA(tb[TCA_GACT_PROB]);
189 } else {
190 /* need to keep consistent output */
191 memset(&pp_dummy, 0, sizeof(pp_dummy));
192 pp = &pp_dummy;
193 }
194 fprintf(f, "\n\t random type %s", prob_n2a(pp->ptype));
195 print_action_control(f, " ", pp->paction, " ");
196 fprintf(f, "val %d", pp->pval);
197 #endif
198 fprintf(f, "\n\t index %u ref %d bind %d", p->index, p->refcnt,
199 p->bindcnt);
200 if (show_stats) {
201 if (tb[TCA_GACT_TM]) {
202 struct tcf_t *tm = RTA_DATA(tb[TCA_GACT_TM]);
203
204 print_tm(f, tm);
205 }
206 }
207 fprintf(f, "\n ");
208 return 0;
209 }
210
211 struct action_util gact_action_util = {
212 .id = "gact",
213 .parse_aopt = parse_gact,
214 .print_aopt = print_gact,
215 };