]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/m_gact.c
tc: jsonify actions core
[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
90
91 if (matches(*argv, "gact") == 0) {
18f05d06
JP
92 argc--;
93 argv++;
73aa9888
PS
94 } else if (parse_action_control(&argc, &argv, &p.action, false) == -1) {
95 usage(); /* does not return */
2265da08
SH
96 }
97
2265da08 98#ifdef CONFIG_GACT_PROB
73aa9888 99 if (argc > 0) {
2265da08
SH
100 if (matches(*argv, "random") == 0) {
101 rd = 1;
102 NEXT_ARG();
103 if (matches(*argv, "netrand") == 0) {
104 NEXT_ARG();
105 pp.ptype = PGACT_NETRAND;
106 } else if (matches(*argv, "determ") == 0) {
107 NEXT_ARG();
108 pp.ptype = PGACT_DETERM;
109 } else {
110 fprintf(stderr, "Illegal \"random type\"\n");
111 return -1;
112 }
113
e67aba55
JP
114 if (parse_action_control(&argc, &argv,
115 &pp.paction, false) == -1)
116 usage();
2265da08 117 if (get_u16(&pp.pval, *argv, 10)) {
32a121cb 118 fprintf(stderr, "Illegal probability val 0x%x\n", pp.pval);
2265da08
SH
119 return -1;
120 }
121 if (pp.pval > 10000) {
32a121cb 122 fprintf(stderr, "Illegal probability val 0x%x\n", pp.pval);
2265da08
SH
123 return -1;
124 }
125 argc--;
126 argv++;
ebf32083
JHS
127 } else if (matches(*argv, "help") == 0) {
128 usage();
2265da08
SH
129 }
130 }
131#endif
132
133 if (argc > 0) {
134 if (matches(*argv, "index") == 0) {
135 NEXT_ARG();
136 if (get_u32(&p.index, *argv, 10)) {
137 fprintf(stderr, "Illegal \"index\"\n");
138 return -1;
139 }
140 argc--;
141 argv++;
f1e4f042 142 } else if (matches(*argv, "help") == 0) {
302d3fb7 143 usage();
2265da08
SH
144 }
145 }
146
228569c3 147 tail = NLMSG_TAIL(n);
2265da08 148 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
32a121cb 149 addattr_l(n, MAX_MSG, TCA_GACT_PARMS, &p, sizeof(p));
2265da08
SH
150#ifdef CONFIG_GACT_PROB
151 if (rd) {
32a121cb 152 addattr_l(n, MAX_MSG, TCA_GACT_PROB, &pp, sizeof(pp));
2265da08
SH
153 }
154#endif
228569c3 155 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
2265da08
SH
156
157 *argc_p = argc;
158 *argv_p = argv;
159 return 0;
160}
161
d1f28cf1 162static int
32a121cb 163print_gact(struct action_util *au, FILE * f, struct rtattr *arg)
2265da08 164{
2265da08 165#ifdef CONFIG_GACT_PROB
2265da08
SH
166 struct tc_gact_p *pp = NULL;
167 struct tc_gact_p pp_dummy;
168#endif
169 struct tc_gact *p = NULL;
170 struct rtattr *tb[TCA_GACT_MAX + 1];
171
172 if (arg == NULL)
173 return -1;
174
3b3ecd31 175 parse_rtattr_nested(tb, TCA_GACT_MAX, arg);
2265da08
SH
176
177 if (tb[TCA_GACT_PARMS] == NULL) {
178 fprintf(f, "[NULL gact parameters]");
179 return -1;
180 }
181 p = RTA_DATA(tb[TCA_GACT_PARMS]);
182
e67aba55
JP
183 fprintf(f, "gact ");
184 print_action_control(f, "action ", p->action, "");
2265da08 185#ifdef CONFIG_GACT_PROB
32a121cb 186 if (tb[TCA_GACT_PROB] != NULL) {
2265da08
SH
187 pp = RTA_DATA(tb[TCA_GACT_PROB]);
188 } else {
189 /* need to keep consistent output */
32a121cb 190 memset(&pp_dummy, 0, sizeof(pp_dummy));
2265da08
SH
191 pp = &pp_dummy;
192 }
e67aba55
JP
193 fprintf(f, "\n\t random type %s", prob_n2a(pp->ptype));
194 print_action_control(f, " ", pp->paction, " ");
195 fprintf(f, "val %d", pp->pval);
2265da08 196#endif
53075318
RM
197 fprintf(f, "\n\t index %u ref %d bind %d", p->index, p->refcnt,
198 p->bindcnt);
2265da08
SH
199 if (show_stats) {
200 if (tb[TCA_GACT_TM]) {
201 struct tcf_t *tm = RTA_DATA(tb[TCA_GACT_TM]);
32a121cb
SH
202
203 print_tm(f, tm);
2265da08
SH
204 }
205 }
206 fprintf(f, "\n ");
207 return 0;
208}
209
95812b56 210struct action_util gact_action_util = {
2265da08
SH
211 .id = "gact",
212 .parse_aopt = parse_gact,
213 .print_aopt = print_gact,
2265da08 214};