]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/m_gact.c
devlink: Add support for resource/dpipe relation
[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>
2265da08
SH
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
ae665a52 29static const char *prob_n2a(int p)
2265da08
SH
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
41static void
42explain(void)
43{
44#ifdef CONFIG_GACT_PROB
45 fprintf(stderr, "Usage: ... gact <ACTION> [RAND] [INDEX]\n");
46 fprintf(stderr,
d19f72f7 47 "Where: \tACTION := reclassify | drop | continue | pass | pipe |\n"
35f2a763 48 " \t goto chain <CHAIN_INDEX> | jump <JUMP_COUNT>\n"
32a121cb
SH
49 "\tRAND := random <RANDTYPE> <ACTION> <VAL>\n"
50 "\tRANDTYPE := netrand | determ\n"
ebf32083 51 "\tVAL : = value not exceeding 10000\n"
35f2a763 52 "\tJUMP_COUNT := Absolute jump from start of action list\n"
ebf32083 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 58 "Where: \tACTION := reclassify | drop | continue | pass | pipe |\n"
35f2a763 59 " \t goto chain <CHAIN_INDEX> | jump <JUMP_COUNT>\n"
ebf32083 60 "\tINDEX := index value used\n"
35f2a763 61 "\tJUMP_COUNT := Absolute jump from start of action list\n"
f1e4f042 62 "\n");
2265da08
SH
63#endif
64}
65
302d3fb7 66
ebf32083
JHS
67static void
68usage(void)
69{
70 explain();
71 exit(-1);
72}
73
d1f28cf1
SH
74static int
75parse_gact(struct action_util *a, int *argc_p, char ***argv_p,
76 int tca_id, struct nlmsghdr *n)
2265da08
SH
77{
78 int argc = *argc_p;
79 char **argv = *argv_p;
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
3572e01a
MP
90 if (matches(*argv, "gact") != 0 &&
91 parse_action_control(&argc, &argv, &p.action, false) == -1) {
73aa9888 92 usage(); /* does not return */
2265da08
SH
93 }
94
3572e01a
MP
95 NEXT_ARG_FWD();
96
2265da08 97#ifdef CONFIG_GACT_PROB
73aa9888 98 if (argc > 0) {
2265da08
SH
99 if (matches(*argv, "random") == 0) {
100 rd = 1;
101 NEXT_ARG();
102 if (matches(*argv, "netrand") == 0) {
103 NEXT_ARG();
104 pp.ptype = PGACT_NETRAND;
105 } else if (matches(*argv, "determ") == 0) {
106 NEXT_ARG();
107 pp.ptype = PGACT_DETERM;
108 } else {
109 fprintf(stderr, "Illegal \"random type\"\n");
110 return -1;
111 }
112
e67aba55
JP
113 if (parse_action_control(&argc, &argv,
114 &pp.paction, false) == -1)
115 usage();
3572e01a 116 NEXT_ARG_FWD();
2265da08 117 if (get_u16(&pp.pval, *argv, 10)) {
5c235ac2
SH
118 fprintf(stderr,
119 "Illegal probability val 0x%x\n",
120 pp.pval);
2265da08
SH
121 return -1;
122 }
123 if (pp.pval > 10000) {
5c235ac2
SH
124 fprintf(stderr,
125 "Illegal probability val 0x%x\n",
126 pp.pval);
2265da08
SH
127 return -1;
128 }
129 argc--;
130 argv++;
ebf32083 131 } else if (matches(*argv, "help") == 0) {
5c235ac2 132 usage();
2265da08
SH
133 }
134 }
135#endif
136
137 if (argc > 0) {
138 if (matches(*argv, "index") == 0) {
139 NEXT_ARG();
140 if (get_u32(&p.index, *argv, 10)) {
141 fprintf(stderr, "Illegal \"index\"\n");
142 return -1;
143 }
144 argc--;
145 argv++;
f1e4f042 146 } else if (matches(*argv, "help") == 0) {
5c235ac2 147 usage();
2265da08
SH
148 }
149 }
150
228569c3 151 tail = NLMSG_TAIL(n);
2265da08 152 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
32a121cb 153 addattr_l(n, MAX_MSG, TCA_GACT_PARMS, &p, sizeof(p));
2265da08 154#ifdef CONFIG_GACT_PROB
5c235ac2 155 if (rd)
32a121cb 156 addattr_l(n, MAX_MSG, TCA_GACT_PROB, &pp, sizeof(pp));
2265da08 157#endif
228569c3 158 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
2265da08
SH
159
160 *argc_p = argc;
161 *argv_p = argv;
162 return 0;
163}
164
d1f28cf1 165static int
5c235ac2 166print_gact(struct action_util *au, FILE *f, struct rtattr *arg)
2265da08 167{
2265da08 168#ifdef CONFIG_GACT_PROB
2265da08
SH
169 struct tc_gact_p *pp = NULL;
170 struct tc_gact_p pp_dummy;
171#endif
172 struct tc_gact *p = NULL;
173 struct rtattr *tb[TCA_GACT_MAX + 1];
174
175 if (arg == NULL)
176 return -1;
177
3b3ecd31 178 parse_rtattr_nested(tb, TCA_GACT_MAX, arg);
2265da08
SH
179
180 if (tb[TCA_GACT_PARMS] == NULL) {
66fedb6d 181 print_string(PRINT_FP, NULL, "%s", "[NULL gact parameters]");
2265da08
SH
182 return -1;
183 }
184 p = RTA_DATA(tb[TCA_GACT_PARMS]);
185
66fedb6d 186 print_string(PRINT_ANY, "kind", "%s ", "gact");
e67aba55 187 print_action_control(f, "action ", p->action, "");
2265da08 188#ifdef CONFIG_GACT_PROB
32a121cb 189 if (tb[TCA_GACT_PROB] != NULL) {
2265da08
SH
190 pp = RTA_DATA(tb[TCA_GACT_PROB]);
191 } else {
192 /* need to keep consistent output */
32a121cb 193 memset(&pp_dummy, 0, sizeof(pp_dummy));
2265da08
SH
194 pp = &pp_dummy;
195 }
66fedb6d
JP
196 open_json_object("prob");
197 print_string(PRINT_ANY, "random_type", "\n\t random type %s",
198 prob_n2a(pp->ptype));
e67aba55 199 print_action_control(f, " ", pp->paction, " ");
66fedb6d
JP
200 print_int(PRINT_ANY, "val", "val %d", pp->pval);
201 close_json_object();
2265da08 202#endif
66fedb6d
JP
203 print_uint(PRINT_ANY, "index", "\n\t index %u", p->index);
204 print_int(PRINT_ANY, "ref", " ref %d", p->refcnt);
205 print_int(PRINT_ANY, "bind", " bind %d", 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 }
66fedb6d 213 print_string(PRINT_FP, NULL, "%s", "\n");
2265da08
SH
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};