]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - m_gact.c
Import patch gact_iproute-2.6.8_patch
[lisovros/iproute2_canprio.git] / 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 int prob_a2n(unsigned char *n) 
31 {
32         if (!strcmp(n,"none"))
33                 return PGACT_NONE;
34         if (!strcmp(n,"netrand"))
35                 return PGACT_NETRAND;
36         if (!strcmp(n,"determ"))
37                 return PGACT_DETERM;
38
39         return 0;
40 }
41
42 char *prob_n2a(int p) 
43 {
44         if (p == PGACT_NONE)
45                 return "none";
46         if (p == PGACT_NETRAND)
47                 return "netrand";
48         if (p == PGACT_DETERM)
49                 return "determ";
50         return "none";
51 }
52 #endif
53 static void
54 explain(void)
55 {
56 #ifdef CONFIG_GACT_PROB
57         fprintf(stderr, "Usage: ... gact <ACTION> [RAND] [INDEX]\n");
58         fprintf(stderr,
59                 "Where: ACTION := reclassify | drop | continue | pass "
60                         "RAND := random <RANDTYPE> <ACTION> <VAL>"
61                         "RANDTYPE := netrand | determ"
62                         "VAL : = value not exceeding 10000"
63                         "INDEX := index value used"
64                         "\n");
65 #else
66         fprintf(stderr, "Usage: ... gact <ACTION> [INDEX]\n");
67         fprintf(stderr,
68                 "Where: ACTION := reclassify | drop | continue | pass "
69                 "INDEX := index value used"
70                         "\n");
71 #endif
72 }
73
74 #define usage() return(-1)
75
76 int
77 get_act(char ***argv_p)
78 {
79         char **argv = *argv_p;
80
81         if (matches(*argv, "reclassify") == 0) {
82                 return TC_ACT_RECLASSIFY;
83         } else if (matches(*argv, "drop") == 0 || matches(*argv, "shot") == 0) {
84                 return TC_ACT_SHOT;
85         } else if (matches(*argv, "continue") == 0) {
86                 return TC_ACT_UNSPEC;
87         } else if (matches(*argv, "pipe") == 0) {
88                 return TC_ACT_PIPE;
89         } else if (matches(*argv, "pass") == 0 || matches(*argv, "ok") == 0)  {
90                 return TC_ACT_OK;
91         } else {
92                 fprintf(stderr,"bad action type %s\n",*argv);
93                 return -10;
94         }
95 }
96
97 int
98 parse_gact(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
99 {
100         int argc = *argc_p;
101         char **argv = *argv_p;
102         int ok = 0;
103         int action = TC_POLICE_RECLASSIFY;
104         struct tc_gact p;
105 #ifdef CONFIG_GACT_PROB
106         int rd = 0;
107         struct tc_gact_p pp;
108 #endif
109         struct rtattr *tail;
110
111         memset(&p, 0, sizeof (p));
112         p.action = TC_POLICE_RECLASSIFY;
113
114         if (argc < 0)
115                 return -1;
116
117
118         if (matches(*argv, "gact") == 0) {
119                 ok++;
120         } else {
121                 action = get_act(&argv);
122                 if (action != -10) {
123                         p.action = action;
124                         ok++;
125                 } else {
126                         explain();
127                         return action;
128                 }
129         }
130
131         if (ok) {
132                 argc--;
133                 argv++;
134         }
135
136 #ifdef CONFIG_GACT_PROB
137         if (ok && argc > 0) {
138                 if (matches(*argv, "random") == 0) {
139                         rd = 1;
140                         NEXT_ARG();
141                         if (matches(*argv, "netrand") == 0) {
142                                 NEXT_ARG();
143                                 pp.ptype = PGACT_NETRAND;
144                         } else if  (matches(*argv, "determ") == 0) {
145                                 NEXT_ARG();
146                                 pp.ptype = PGACT_DETERM;
147                         } else {
148                                 fprintf(stderr, "Illegal \"random type\"\n");
149                                 return -1;
150                         }
151
152                         action = get_act(&argv);
153                         if (action != -10) { /* FIXME */
154                                 pp.paction = action;
155                         } else {
156                                 explain();
157                                 return -1;
158                         }
159                         argc--;
160                         argv++;
161                         if (get_u16(&pp.pval, *argv, 10)) {
162                                 fprintf(stderr, "Illegal probability val 0x%x\n",pp.pval);
163                                 return -1;
164                         }
165                         if (pp.pval > 10000) {
166                                 fprintf(stderr, "Illegal probability val  0x%x\n",pp.pval);
167                                 return -1;
168                         }
169                         argc--;
170                         argv++;
171                 }
172         }
173 #endif
174
175         if (argc > 0) {
176                 if (matches(*argv, "index") == 0) {
177                         NEXT_ARG();
178                         if (get_u32(&p.index, *argv, 10)) {
179                                 fprintf(stderr, "Illegal \"index\"\n");
180                                 return -1;
181                         }
182                         argc--;
183                         argv++;
184                         ok++;
185                 }
186         }
187
188         if (!ok)
189                 return -1;
190
191         tail = (struct rtattr *) (((void *) n) + NLMSG_ALIGN(n->nlmsg_len));
192         addattr_l(n, MAX_MSG, tca_id, NULL, 0);
193         addattr_l(n, MAX_MSG, TCA_GACT_PARMS, &p, sizeof (p));
194 #ifdef CONFIG_GACT_PROB
195         if (rd) {
196                 addattr_l(n, MAX_MSG, TCA_GACT_PROB, &pp, sizeof (pp));
197         }
198 #endif
199         tail->rta_len =
200             (((void *) n) + NLMSG_ALIGN(n->nlmsg_len)) - (void *) tail;
201
202         *argc_p = argc;
203         *argv_p = argv;
204         return 0;
205 }
206
207 int
208 print_gact(struct action_util *au,FILE * f, struct rtattr *arg)
209 {
210         SPRINT_BUF(b1);
211 #ifdef CONFIG_GACT_PROB
212         SPRINT_BUF(b2);
213         struct tc_gact_p *pp = NULL;
214         struct tc_gact_p pp_dummy;
215 #endif
216         struct tc_gact *p = NULL;
217         struct rtattr *tb[TCA_GACT_MAX + 1];
218
219         if (arg == NULL)
220                 return -1;
221
222         memset(tb, 0, sizeof (tb));
223         parse_rtattr(tb, TCA_GACT_MAX, RTA_DATA(arg), RTA_PAYLOAD(arg));
224
225         if (tb[TCA_GACT_PARMS] == NULL) {
226                 fprintf(f, "[NULL gact parameters]");
227                 return -1;
228         }
229         p = RTA_DATA(tb[TCA_GACT_PARMS]);
230
231         fprintf(f, "gact action %s", action_n2a(p->action, b1, sizeof (b1)));
232 #ifdef CONFIG_GACT_PROB
233         if (NULL != tb[TCA_GACT_PROB]) {
234                 pp = RTA_DATA(tb[TCA_GACT_PROB]);
235         } else {
236                 /* need to keep consistent output */
237                 memset(&pp_dummy, 0, sizeof (pp_dummy));
238                 pp = &pp_dummy;
239         }
240         fprintf(f, "\n\t random type %s %s val %d",prob_n2a(pp->ptype), action_n2a(pp->paction, b2, sizeof (b2)), pp->pval);
241 #endif
242         fprintf(f, "\n\t index %d ref %d bind %d",p->index, p->refcnt, p->bindcnt);
243         if (show_stats) {
244                 if (tb[TCA_GACT_TM]) {
245                         struct tcf_t *tm = RTA_DATA(tb[TCA_GACT_TM]);
246                         print_tm(f,tm);
247                 }
248         }
249         fprintf(f, "\n ");
250         return 0;
251 }
252
253 int 
254 gact_print_xstats(struct action_util *au, FILE *f, struct rtattr *xstats)
255 {
256         return 0;
257 }
258
259
260 struct action_util gact_util = {
261         NULL,
262         "gact",
263         parse_gact,
264         print_gact,
265         gact_print_xstats,
266 };