2 * m_gact.c generic actions module
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.
9 * Authors: J Hadi Salim (hadi@cyberus.ca)
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
24 #include <linux/tc_act/tc_gact.h>
26 /* define to turn on probablity stuff */
28 #ifdef CONFIG_GACT_PROB
29 static const char *prob_n2a(int p)
33 if (p == PGACT_NETRAND)
35 if (p == PGACT_DETERM)
44 #ifdef CONFIG_GACT_PROB
45 fprintf(stderr, "Usage: ... gact <ACTION> [RAND] [INDEX]\n");
47 "Where: \tACTION := reclassify | drop | continue | pass | pipe |\n"
48 " \t goto chain <CHAIN_INDEX> | jump <JUMP_COUNT>\n"
49 "\tRAND := random <RANDTYPE> <ACTION> <VAL>\n"
50 "\tRANDTYPE := netrand | determ\n"
51 "\tVAL : = value not exceeding 10000\n"
52 "\tJUMP_COUNT := Absolute jump from start of action list\n"
53 "\tINDEX := index value used\n"
56 fprintf(stderr, "Usage: ... gact <ACTION> [INDEX]\n"
57 "Where: \tACTION := reclassify | drop | continue | pass | pipe |\n"
58 " \t goto chain <CHAIN_INDEX> | jump <JUMP_COUNT>\n"
59 "\tINDEX := index value used\n"
60 "\tJUMP_COUNT := Absolute jump from start of action list\n"
74 parse_gact(struct action_util *a, int *argc_p, char ***argv_p,
75 int tca_id, struct nlmsghdr *n)
78 char **argv = *argv_p;
79 struct tc_gact p = { 0 };
80 #ifdef CONFIG_GACT_PROB
89 if (!matches(*argv, "gact"))
91 /* we're binding existing gact action to filter by index. */
92 if (!matches(*argv, "index"))
94 if (parse_action_control(&argc, &argv, &p.action, false))
95 usage(); /* does not return */
97 #ifdef CONFIG_GACT_PROB
99 if (matches(*argv, "random") == 0) {
102 if (matches(*argv, "netrand") == 0) {
104 pp.ptype = PGACT_NETRAND;
105 } else if (matches(*argv, "determ") == 0) {
107 pp.ptype = PGACT_DETERM;
109 fprintf(stderr, "Illegal \"random type\"\n");
113 if (parse_action_control(&argc, &argv,
114 &pp.paction, false) == -1)
116 if (get_u16(&pp.pval, *argv, 10)) {
118 "Illegal probability val 0x%x\n",
122 if (pp.pval > 10000) {
124 "Illegal probability val 0x%x\n",
130 } else if (matches(*argv, "help") == 0) {
137 if (matches(*argv, "index") == 0) {
140 if (get_u32(&p.index, *argv, 10)) {
141 fprintf(stderr, "Illegal \"index\"\n");
146 } else if (matches(*argv, "help") == 0) {
151 tail = addattr_nest(n, MAX_MSG, tca_id);
152 addattr_l(n, MAX_MSG, TCA_GACT_PARMS, &p, sizeof(p));
153 #ifdef CONFIG_GACT_PROB
155 addattr_l(n, MAX_MSG, TCA_GACT_PROB, &pp, sizeof(pp));
157 addattr_nest_end(n, tail);
165 print_gact(struct action_util *au, FILE *f, struct rtattr *arg)
167 #ifdef CONFIG_GACT_PROB
168 struct tc_gact_p *pp = NULL;
169 struct tc_gact_p pp_dummy;
171 struct tc_gact *p = NULL;
172 struct rtattr *tb[TCA_GACT_MAX + 1];
177 parse_rtattr_nested(tb, TCA_GACT_MAX, arg);
179 if (tb[TCA_GACT_PARMS] == NULL) {
180 fprintf(stderr, "Missing gact parameters\n");
183 p = RTA_DATA(tb[TCA_GACT_PARMS]);
185 print_string(PRINT_ANY, "kind", "%s ", "gact");
186 print_action_control(f, "action ", p->action, "");
187 #ifdef CONFIG_GACT_PROB
188 if (tb[TCA_GACT_PROB] != NULL) {
189 pp = RTA_DATA(tb[TCA_GACT_PROB]);
191 /* need to keep consistent output */
192 memset(&pp_dummy, 0, sizeof(pp_dummy));
195 open_json_object("prob");
196 print_string(PRINT_ANY, "random_type", "\n\t random type %s",
197 prob_n2a(pp->ptype));
198 print_action_control(f, " ", pp->paction, " ");
199 print_int(PRINT_ANY, "val", "val %d", pp->pval);
202 print_uint(PRINT_ANY, "index", "\n\t index %u", p->index);
203 print_int(PRINT_ANY, "ref", " ref %d", p->refcnt);
204 print_int(PRINT_ANY, "bind", " bind %d", p->bindcnt);
206 if (tb[TCA_GACT_TM]) {
207 struct tcf_t *tm = RTA_DATA(tb[TCA_GACT_TM]);
212 print_string(PRINT_FP, NULL, "%s", "\n");
216 struct action_util gact_action_util = {
218 .parse_aopt = parse_gact,
219 .print_aopt = print_gact,