2 * net/sched/act_api.c Packet action API.
4 * This program is free software; you can redistribute 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 * Author: Jamal Hadi Salim
14 #include <asm/uaccess.h>
15 #include <asm/system.h>
16 #include <asm/bitops.h>
17 #include <linux/config.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/string.h>
23 #include <linux/socket.h>
24 #include <linux/sockios.h>
26 #include <linux/errno.h>
27 #include <linux/interrupt.h>
28 #include <linux/netdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/init.h>
32 #include <linux/kmod.h>
34 #include <net/pkt_sched.h>
37 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
39 #define DPRINTK(format,args...)
42 #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
44 #define D2PRINTK(format,args...)
47 static struct tc_action_ops *act_base = NULL;
48 static rwlock_t act_mod_lock = RW_LOCK_UNLOCKED;
50 int tcf_register_action(struct tc_action_ops *act)
52 struct tc_action_ops *a, **ap;
54 write_lock(&act_mod_lock);
55 for (ap = &act_base; (a=*ap)!=NULL; ap = &a->next) {
56 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
57 write_unlock(&act_mod_lock);
65 write_unlock(&act_mod_lock);
70 int tcf_unregister_action(struct tc_action_ops *act)
72 struct tc_action_ops *a, **ap;
75 write_lock(&act_mod_lock);
76 for (ap = &act_base; (a=*ap)!=NULL; ap = &a->next)
85 write_unlock(&act_mod_lock);
90 struct tc_action_ops *tc_lookup_action_n(char *kind)
93 struct tc_action_ops *a = NULL;
96 read_lock(&act_mod_lock);
97 for (a = act_base; a; a = a->next) {
98 if (strcmp(kind,a->kind) == 0) {
99 if (!try_module_get(a->owner)) {
100 read_unlock(&act_mod_lock);
106 read_unlock(&act_mod_lock);
112 /* lookup by rtattr */
113 struct tc_action_ops *tc_lookup_action(struct rtattr *kind)
116 struct tc_action_ops *a = NULL;
119 read_lock(&act_mod_lock);
120 for (a = act_base; a; a = a->next) {
122 if (strcmp((char*)RTA_DATA(kind),a->kind) == 0){
123 if (!try_module_get(a->owner)) {
124 read_unlock(&act_mod_lock);
130 read_unlock(&act_mod_lock);
137 struct tc_action_ops *tc_lookup_action_id(u32 type)
139 struct tc_action_ops *a = NULL;
142 read_lock(&act_mod_lock);
143 for (a = act_base; a; a = a->next) {
144 if (a->type == type) {
145 if (!try_module_get(a->owner)) {
146 read_unlock(&act_mod_lock);
152 read_unlock(&act_mod_lock);
158 int tcf_action_exec(struct sk_buff *skb,struct tc_action *act)
164 if (skb->tc_verd & TC_NCLS) {
165 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
166 D2PRINTK("(%p)tcf_action_exec: cleared TC_NCLS in %s out %s\n",skb,skb->input_dev?skb->input_dev->name:"xxx",skb->dev->name);
169 while ((a = act) != NULL) {
171 if (a->ops && a->ops->act) {
172 ret = a->ops->act(&skb,a);
173 if (TC_MUNGED & skb->tc_verd) {
174 /* copied already, allow trampling */
175 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
176 skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
179 if (ret != TC_ACT_PIPE)
181 if (ret == TC_ACT_REPEAT)
182 goto repeat; /* we need a ttl - JHS */
193 void tcf_action_destroy(struct tc_action *act, int bind)
197 for (a = act; act; a = act) {
198 if (a && a->ops && a->ops->cleanup) {
199 DPRINTK("tcf_action_destroy destroying %p next %p\n", a,a->next?a->next:NULL);
201 if (ACT_P_DELETED == a->ops->cleanup(a, bind)) {
202 module_put(a->ops->owner);
207 } else { /*FIXME: Remove later - catch insertion bugs*/
208 printk("tcf_action_destroy: BUG? destroying NULL ops \n");
213 printk("tcf_action_destroy: BUG? destroying NULL action! \n");
220 int tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
225 if ( (NULL == a) || (NULL == a->ops)
226 || (NULL == a->ops->dump) )
228 return a->ops->dump(skb, a, bind, ref);
233 int tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
236 unsigned char *b = skb->tail;
240 if ( (NULL == a) || (NULL == a->ops)
241 || (NULL == a->ops->dump) || (NULL == a->ops->kind))
245 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind);
246 if (tcf_action_copy_stats(skb,a))
248 r = (struct rtattr*) skb->tail;
249 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
250 if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
251 r->rta_len = skb->tail - (u8*)r;
257 skb_trim(skb, b - skb->data);
262 int tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
266 unsigned char *b = skb->tail;
269 while ((a = act) != NULL) {
270 r = (struct rtattr*) skb->tail;
272 RTA_PUT(skb, a->order, 0, NULL);
273 err = tcf_action_dump_1(skb, a, bind, ref);
277 r->rta_len = skb->tail - (u8*)r;
283 skb_trim(skb, b - skb->data);
288 int tcf_action_init_1(struct rtattr *rta, struct rtattr *est, struct tc_action *a, char *name, int ovr, int bind )
290 struct tc_action_ops *a_o;
291 char act_name[4 + IFNAMSIZ + 1];
292 struct rtattr *tb[TCA_ACT_MAX+1];
293 struct rtattr *kind = NULL;
298 if (rtattr_parse(tb, TCA_ACT_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta))<0)
300 kind = tb[TCA_ACT_KIND-1];
302 sprintf(act_name, "%s", (char*)RTA_DATA(kind));
303 if (RTA_PAYLOAD(kind) >= IFNAMSIZ) {
304 printk(" Action %s bad\n", (char*)RTA_DATA(kind));
309 printk("Action bad kind\n");
312 a_o = tc_lookup_action(kind);
314 sprintf(act_name, "%s", name);
315 DPRINTK("tcf_action_init_1: finding %s\n",act_name);
316 a_o = tc_lookup_action_n(name);
320 DPRINTK("tcf_action_init_1: trying to load module %s\n",act_name);
321 request_module (act_name);
322 a_o = tc_lookup_action_n(act_name);
327 printk("failed to find %s\n",act_name);
335 /* backward compatibility for policer */
337 err = a_o->init(tb[TCA_ACT_OPTIONS-1], est, a, ovr, bind);
343 err = a_o->init(rta, est, a, ovr, bind);
350 /* module count goes up only when brand new policy is created
351 if it exists and is only bound to in a_o->init() then
352 ACT_P_CREATED is not returned (a zero is).
354 if (ACT_P_CREATED != err) {
355 module_put(a_o->owner);
358 DPRINTK("tcf_action_init_1: successfull %s \n",act_name);
362 module_put(a_o->owner);
367 int tcf_action_init(struct rtattr *rta, struct rtattr *est, struct tc_action *a, char *name, int ovr , int bind)
369 struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
371 struct tc_action *act = a, *a_s = a;
375 if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(rta), RTA_PAYLOAD(rta))<0)
378 for (i=0; i < TCA_ACT_MAX_PRIO ; i++) {
381 act = kmalloc(sizeof(*act),GFP_KERNEL);
386 memset(act, 0,sizeof(*act));
389 if (0 > tcf_action_init_1(tb[i],est,act,name,ovr,bind)) {
390 printk("Error processing action order %d\n",i);
406 tcf_action_destroy(a, bind);
410 int tcf_action_copy_stats (struct sk_buff *skb,struct tc_action *a)
416 if (NULL == a->ops || NULL == a->ops->get_stats)
419 return a->ops->get_stats(skb,a);
424 tca_get_fill(struct sk_buff *skb, struct tc_action *a,
425 u32 pid, u32 seq, unsigned flags, int event, int bind, int ref)
428 struct nlmsghdr *nlh;
429 unsigned char *b = skb->tail;
432 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*t));
433 nlh->nlmsg_flags = flags;
435 t->tca_family = AF_UNSPEC;
437 x = (struct rtattr*) skb->tail;
438 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
440 if (0 > tcf_action_dump(skb, a, bind, ref)) {
444 x->rta_len = skb->tail - (u8*)x;
446 nlh->nlmsg_len = skb->tail - b;
451 skb_trim(skb, b - skb->data);
455 static int act_get_notify(u32 pid, struct nlmsghdr *n,
456 struct tc_action *a, int event)
462 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
466 if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
471 err = netlink_unicast(rtnl,skb, pid, MSG_DONTWAIT);
477 int tcf_action_get_1(struct rtattr *rta, struct tc_action *a, struct nlmsghdr *n, u32 pid)
479 struct tc_action_ops *a_o;
480 char act_name[4 + IFNAMSIZ + 1];
481 struct rtattr *tb[TCA_ACT_MAX+1];
482 struct rtattr *kind = NULL;
487 if (rtattr_parse(tb, TCA_ACT_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta))<0)
491 kind = tb[TCA_ACT_KIND-1];
493 sprintf(act_name, "%s", (char*)RTA_DATA(kind));
494 if (RTA_PAYLOAD(kind) >= IFNAMSIZ) {
495 printk("tcf_action_get_1: action %s bad\n", (char*)RTA_DATA(kind));
500 printk("tcf_action_get_1: action bad kind\n");
504 if (tb[TCA_ACT_INDEX - 1]) {
505 index = *(int *)RTA_DATA(tb[TCA_ACT_INDEX - 1]);
507 printk("tcf_action_get_1: index not received\n");
511 a_o = tc_lookup_action(kind);
514 request_module (act_name);
515 a_o = tc_lookup_action_n(act_name);
520 printk("failed to find %s\n",act_name);
530 if (NULL == a_o->lookup || 0 == a_o->lookup(a, index)) {
536 module_put(a_o->owner);
539 module_put(a_o->owner);
544 void cleanup_a (struct tc_action *act)
548 for (a = act; act; a = act) {
555 printk("cleanup_a: BUG? empty action\n");
560 struct tc_action_ops *get_ao(struct rtattr *kind, struct tc_action *a)
562 char act_name[4 + IFNAMSIZ + 1];
563 struct tc_action_ops *a_o = NULL;
566 sprintf(act_name, "%s", (char*)RTA_DATA(kind));
567 if (RTA_PAYLOAD(kind) >= IFNAMSIZ) {
568 printk("get_ao: action %s bad\n", (char*)RTA_DATA(kind));
573 printk("get_ao: action bad kind\n");
577 a_o = tc_lookup_action(kind);
580 DPRINTK("get_ao: trying to load module %s\n",act_name);
581 request_module (act_name);
582 a_o = tc_lookup_action_n(act_name);
587 printk("get_ao: failed to find %s\n",act_name);
595 struct tc_action *create_a(int i)
597 struct tc_action *act = NULL;
599 act = kmalloc(sizeof(*act),GFP_KERNEL);
600 if (NULL == act) { /* grrr .. */
601 printk("create_a: failed to alloc! \n");
605 memset(act, 0,sizeof(*act));
612 int tca_action_flush(struct rtattr *rta, struct nlmsghdr *n, u32 pid)
616 struct nlmsghdr *nlh;
618 struct netlink_callback dcb;
620 struct rtattr *tb[TCA_ACT_MAX+1];
621 struct rtattr *kind = NULL;
622 struct tc_action *a = create_a(0);
626 printk("tca_action_flush: couldnt create tc_action\n");
630 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
632 printk("tca_action_flush: failed skb alloc\n");
637 b = (unsigned char *)skb->tail;
639 if (rtattr_parse(tb, TCA_ACT_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta))<0) {
643 kind = tb[TCA_ACT_KIND-1];
644 if (NULL == get_ao(kind, a)) {
648 nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof (*t));
650 t->tca_family = AF_UNSPEC;
652 x = (struct rtattr *) skb->tail;
653 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
655 err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
660 x->rta_len = skb->tail - (u8 *) x;
662 nlh->nlmsg_len = skb->tail - b;
663 nlh->nlmsg_flags |= NLM_F_ROOT;
664 module_put(a->ops->owner);
666 err = rtnetlink_send(skb, pid, RTMGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
674 module_put(a->ops->owner);
682 int tca_action_gd(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int event )
687 struct tc_action *act = NULL;
688 struct rtattr *tb[TCA_ACT_MAX_PRIO+1];
689 struct tc_action *a = NULL, *a_s = NULL;
691 if (event != RTM_GETACTION && event != RTM_DELACTION)
694 if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(rta), RTA_PAYLOAD(rta))<0) {
699 if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
700 if (NULL != tb[0] && NULL == tb[1]) {
701 return tca_action_flush(tb[0],n,pid);
705 for (i=0; i < TCA_ACT_MAX_PRIO ; i++) {
711 if (NULL != a && a != act) {
723 ret = tcf_action_get_1(tb[i],act,n,pid);
725 printk("tcf_action_get: failed to get! \n");
733 if (RTM_GETACTION == event) {
734 ret = act_get_notify(pid, n, a_s, event);
735 } else { /* delete */
739 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
745 if (tca_get_fill(skb, a_s, pid, n->nlmsg_seq, 0, event, 0 , 1) <= 0) {
751 /* now do the delete */
752 tcf_action_destroy(a_s, 0);
754 ret = rtnetlink_send(skb, pid, RTMGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
766 int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event, unsigned flags)
769 struct nlmsghdr *nlh;
777 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
781 b = (unsigned char *)skb->tail;
783 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*t));
784 nlh->nlmsg_flags = flags;
786 t->tca_family = AF_UNSPEC;
788 x = (struct rtattr*) skb->tail;
789 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
791 if (0 > tcf_action_dump(skb, a, 0, 0)) {
795 x->rta_len = skb->tail - (u8*)x;
797 nlh->nlmsg_len = skb->tail - b;
798 NETLINK_CB(skb).dst_groups = RTMGRP_TC;
800 err = rtnetlink_send(skb, pid, RTMGRP_TC, flags&NLM_F_ECHO);
808 skb_trim(skb, b - skb->data);
813 int tcf_action_add(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int ovr )
816 struct tc_action *act = NULL;
817 struct tc_action *a = NULL;
818 u32 seq = n->nlmsg_seq;
820 act = kmalloc(sizeof(*act),GFP_KERNEL);
824 memset(act, 0, sizeof(*act));
826 ret = tcf_action_init(rta, NULL,act,NULL,ovr,0);
827 /* NOTE: We have an all-or-none model
828 * This means that of any of the actions fail
829 * to update then all are undone.
832 tcf_action_destroy(act, 0);
836 /* dump then free all the actions after update; inserted policy
839 ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
840 for (a = act; act; a = act) {
847 printk("tcf_action_add: BUG? empty action\n");
855 static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
857 struct rtattr **tca = arg;
858 u32 pid = skb ? NETLINK_CB(skb).pid : 0;
860 int ret = 0, ovr = 0;
862 if (NULL == tca[TCA_ACT_TAB-1]) {
863 printk("tc_ctl_action: received NO action attribs\n");
867 /* n->nlmsg_flags&NLM_F_CREATE
869 switch (n->nlmsg_type) {
871 /* we are going to assume all other flags
872 * imply create only if it doesnt exist
873 * Note that CREATE | EXCL implies that
874 * but since we want avoid ambiguity (eg when flags
875 * is zero) then just set this
877 if (n->nlmsg_flags&NLM_F_REPLACE) {
880 ret = tcf_action_add(tca[TCA_ACT_TAB-1], n, pid, ovr);
883 ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid,RTM_DELACTION);
886 ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid,RTM_GETACTION);
889 printk(" Unknown cmd was detected\n");
897 find_dump_kind(struct nlmsghdr *n)
899 struct rtattr *tb1, *tb2[TCA_ACT_MAX+1];
900 struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
901 struct rtattr *rta[TCAA_MAX + 1];
902 struct rtattr *kind = NULL;
903 int min_len = NLMSG_LENGTH(sizeof (struct tcamsg));
905 int attrlen = n->nlmsg_len - NLMSG_ALIGN(min_len);
906 struct rtattr *attr = (void *) n + NLMSG_ALIGN(min_len);
908 if (rtattr_parse(rta, TCAA_MAX, attr, attrlen) < 0)
910 tb1 = rta[TCA_ACT_TAB - 1];
915 if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(tb1), NLMSG_ALIGN(RTA_PAYLOAD(tb1))) < 0)
920 if (rtattr_parse(tb2, TCA_ACT_MAX, RTA_DATA(tb[0]), RTA_PAYLOAD(tb[0]))<0)
922 kind = tb2[TCA_ACT_KIND-1];
924 return (char *) RTA_DATA(kind);
928 tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
930 struct nlmsghdr *nlh;
931 unsigned char *b = skb->tail;
933 struct tc_action_ops *a_o;
937 struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
938 char *kind = find_dump_kind(cb->nlh);
940 printk("tc_dump_action: action bad kind\n");
944 a_o = tc_lookup_action_n(kind);
947 printk("failed to find %s\n", kind);
951 memset(&a,0,sizeof(struct tc_action));
954 if (NULL == a_o->walk) {
955 printk("tc_dump_action: %s !capable of dumping table\n",kind);
959 nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, cb->nlh->nlmsg_type, sizeof (*t));
961 t->tca_family = AF_UNSPEC;
963 x = (struct rtattr *) skb->tail;
964 RTA_PUT(skb, TCA_ACT_TAB, 0, NULL);
966 ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
972 x->rta_len = skb->tail - (u8 *) x;
975 skb_trim(skb, (u8*)x - skb->data);
978 nlh->nlmsg_len = skb->tail - b;
979 if (NETLINK_CB(cb->skb).pid && ret)
980 nlh->nlmsg_flags |= NLM_F_MULTI;
981 module_put(a_o->owner);
986 module_put(a_o->owner);
987 skb_trim(skb, b - skb->data);
991 static int __init tc_action_init(void)
993 struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
996 link_p[RTM_NEWACTION-RTM_BASE].doit = tc_ctl_action;
997 link_p[RTM_DELACTION-RTM_BASE].doit = tc_ctl_action;
998 link_p[RTM_GETACTION-RTM_BASE].doit = tc_ctl_action;
999 link_p[RTM_GETACTION-RTM_BASE].dumpit = tc_dump_action;
1002 printk("TC classifier action (bugs to netdev@oss.sgi.com cc hadi@cyberus.ca)\n");
1006 subsys_initcall(tc_action_init);
1008 EXPORT_SYMBOL(tcf_register_action);
1009 EXPORT_SYMBOL(tcf_unregister_action);
1010 EXPORT_SYMBOL(tcf_action_init_1);
1011 EXPORT_SYMBOL(tcf_action_init);
1012 EXPORT_SYMBOL(tcf_action_destroy);
1013 EXPORT_SYMBOL(tcf_action_exec);
1014 EXPORT_SYMBOL(tcf_action_copy_stats);
1015 EXPORT_SYMBOL(tcf_action_dump);
1016 EXPORT_SYMBOL(tcf_action_dump_1);
1017 EXPORT_SYMBOL(tcf_action_dump_old);