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