]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/m_gact.c
ss: Drop useless assignment
[mirror_iproute2.git] / tc / m_gact.c
CommitLineData
2265da08 1/*
ae665a52 2 * m_gact.c generic actions module
2265da08
SH
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 *
ae665a52
SH
9 * Authors: J Hadi Salim (hadi@cyberus.ca)
10 *
2265da08
SH
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
ae665a52 30static const char *prob_n2a(int p)
2265da08
SH
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
42static void
43explain(void)
44{
45#ifdef CONFIG_GACT_PROB
46 fprintf(stderr, "Usage: ... gact <ACTION> [RAND] [INDEX]\n");
47 fprintf(stderr,
d19f72f7
JP
48 "Where: \tACTION := reclassify | drop | continue | pass | pipe |\n"
49 " \t goto chain <CHAIN_INDEX>\n"
32a121cb
SH
50 "\tRAND := random <RANDTYPE> <ACTION> <VAL>\n"
51 "\tRANDTYPE := netrand | determ\n"
ebf32083
JHS
52 "\tVAL : = value not exceeding 10000\n"
53 "\tINDEX := index value used\n"
302d3fb7 54 "\n");
2265da08
SH
55#else
56 fprintf(stderr, "Usage: ... gact <ACTION> [INDEX]\n");
57 fprintf(stderr,
d19f72f7
JP
58 "Where: \tACTION := reclassify | drop | continue | pass | pipe |\n"
59 " \t goto chain <CHAIN_INDEX>\n"
ebf32083 60 "\tINDEX := index value used\n"
f1e4f042 61 "\n");
2265da08
SH
62#endif
63}
64
302d3fb7 65
ebf32083
JHS
66static void
67usage(void)
68{
69 explain();
70 exit(-1);
71}
72
d1f28cf1
SH
73static int
74parse_gact(struct action_util *a, int *argc_p, char ***argv_p,
75 int tca_id, struct nlmsghdr *n)
2265da08
SH
76{
77 int argc = *argc_p;
78 char **argv = *argv_p;
79 int ok = 0;
e67aba55 80 struct tc_gact p = { 0 };
2265da08
SH
81#ifdef CONFIG_GACT_PROB
82 int rd = 0;
83 struct tc_gact_p pp;
84#endif
85 struct rtattr *tail;
86
2265da08
SH
87 if (argc < 0)
88 return -1;
89
90
91 if (matches(*argv, "gact") == 0) {
92 ok++;
18f05d06
JP
93 argc--;
94 argv++;
2265da08 95 } else {
e67aba55
JP
96 if (parse_action_control(&argc, &argv, &p.action, false) == -1)
97 usage();
98 ok++;
2265da08
SH
99 }
100
2265da08
SH
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
e67aba55
JP
117 if (parse_action_control(&argc, &argv,
118 &pp.paction, false) == -1)
119 usage();
2265da08 120 if (get_u16(&pp.pval, *argv, 10)) {
32a121cb 121 fprintf(stderr, "Illegal probability val 0x%x\n", pp.pval);
2265da08
SH
122 return -1;
123 }
124 if (pp.pval > 10000) {
32a121cb 125 fprintf(stderr, "Illegal probability val 0x%x\n", pp.pval);
2265da08
SH
126 return -1;
127 }
128 argc--;
129 argv++;
ebf32083
JHS
130 } else if (matches(*argv, "help") == 0) {
131 usage();
2265da08
SH
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++;
f1e4f042 146 } else if (matches(*argv, "help") == 0) {
302d3fb7 147 usage();
2265da08
SH
148 }
149 }
150
151 if (!ok)
152 return -1;
153
228569c3 154 tail = NLMSG_TAIL(n);
2265da08 155 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
32a121cb 156 addattr_l(n, MAX_MSG, TCA_GACT_PARMS, &p, sizeof(p));
2265da08
SH
157#ifdef CONFIG_GACT_PROB
158 if (rd) {
32a121cb 159 addattr_l(n, MAX_MSG, TCA_GACT_PROB, &pp, sizeof(pp));
2265da08
SH
160 }
161#endif
228569c3 162 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
2265da08
SH
163
164 *argc_p = argc;
165 *argv_p = argv;
166 return 0;
167}
168
d1f28cf1 169static int
32a121cb 170print_gact(struct action_util *au, FILE * f, struct rtattr *arg)
2265da08 171{
2265da08 172#ifdef CONFIG_GACT_PROB
2265da08
SH
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
3b3ecd31 182 parse_rtattr_nested(tb, TCA_GACT_MAX, arg);
2265da08
SH
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
e67aba55
JP
190 fprintf(f, "gact ");
191 print_action_control(f, "action ", p->action, "");
2265da08 192#ifdef CONFIG_GACT_PROB
32a121cb 193 if (tb[TCA_GACT_PROB] != NULL) {
2265da08
SH
194 pp = RTA_DATA(tb[TCA_GACT_PROB]);
195 } else {
196 /* need to keep consistent output */
32a121cb 197 memset(&pp_dummy, 0, sizeof(pp_dummy));
2265da08
SH
198 pp = &pp_dummy;
199 }
e67aba55
JP
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);
2265da08 203#endif
53075318
RM
204 fprintf(f, "\n\t index %u ref %d bind %d", p->index, p->refcnt,
205 p->bindcnt);
2265da08
SH
206 if (show_stats) {
207 if (tb[TCA_GACT_TM]) {
208 struct tcf_t *tm = RTA_DATA(tb[TCA_GACT_TM]);
32a121cb
SH
209
210 print_tm(f, tm);
2265da08
SH
211 }
212 }
213 fprintf(f, "\n ");
214 return 0;
215}
216
95812b56 217struct action_util gact_action_util = {
2265da08
SH
218 .id = "gact",
219 .parse_aopt = parse_gact,
220 .print_aopt = print_gact,
2265da08 221};