2 * net/sched/ife.c Inter-FE action based on ForCES WG InterFE LFB
5 * draft-ietf-forces-interfelfb-03
8 * "Distributing Linux Traffic Control Classifier-Action
10 * Authors: Jamal Hadi Salim and Damascene M. Joachimpillai
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
17 * copyright Jamal Hadi Salim (2015)
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/errno.h>
25 #include <linux/skbuff.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <net/net_namespace.h>
30 #include <net/netlink.h>
31 #include <net/pkt_sched.h>
32 #include <uapi/linux/tc_act/tc_ife.h>
33 #include <net/tc_act/tc_ife.h>
34 #include <linux/etherdevice.h>
36 #define IFE_TAB_MASK 15
38 static unsigned int ife_net_id;
39 static int max_metacnt = IFE_META_MAX + 1;
40 static struct tc_action_ops act_ife_ops;
42 static const struct nla_policy ife_policy[TCA_IFE_MAX + 1] = {
43 [TCA_IFE_PARMS] = { .len = sizeof(struct tc_ife)},
44 [TCA_IFE_DMAC] = { .len = ETH_ALEN},
45 [TCA_IFE_SMAC] = { .len = ETH_ALEN},
46 [TCA_IFE_TYPE] = { .type = NLA_U16},
49 /* Caller takes care of presenting data in network order
51 int ife_tlv_meta_encode(void *skbdata, u16 attrtype, u16 dlen, const void *dval)
53 u32 *tlv = (u32 *)(skbdata);
54 u16 totlen = nla_total_size(dlen); /*alignment + hdr */
55 char *dptr = (char *)tlv + NLA_HDRLEN;
56 u32 htlv = attrtype << 16 | (dlen + NLA_HDRLEN);
59 memset(dptr, 0, totlen - NLA_HDRLEN);
60 memcpy(dptr, dval, dlen);
64 EXPORT_SYMBOL_GPL(ife_tlv_meta_encode);
66 int ife_encode_meta_u16(u16 metaval, void *skbdata, struct tcf_meta_info *mi)
71 edata = *(u16 *)mi->metaval;
75 if (!edata) /* will not encode */
79 return ife_tlv_meta_encode(skbdata, mi->metaid, 2, &edata);
81 EXPORT_SYMBOL_GPL(ife_encode_meta_u16);
83 int ife_get_meta_u32(struct sk_buff *skb, struct tcf_meta_info *mi)
86 return nla_put_u32(skb, mi->metaid, *(u32 *)mi->metaval);
88 return nla_put(skb, mi->metaid, 0, NULL);
90 EXPORT_SYMBOL_GPL(ife_get_meta_u32);
92 int ife_check_meta_u32(u32 metaval, struct tcf_meta_info *mi)
94 if (metaval || mi->metaval)
95 return 8; /* T+L+V == 2+2+4 */
99 EXPORT_SYMBOL_GPL(ife_check_meta_u32);
101 int ife_check_meta_u16(u16 metaval, struct tcf_meta_info *mi)
103 if (metaval || mi->metaval)
104 return 8; /* T+L+(V) == 2+2+(2+2bytepad) */
108 EXPORT_SYMBOL_GPL(ife_check_meta_u16);
110 int ife_encode_meta_u32(u32 metaval, void *skbdata, struct tcf_meta_info *mi)
115 edata = *(u32 *)mi->metaval;
119 if (!edata) /* will not encode */
122 edata = htonl(edata);
123 return ife_tlv_meta_encode(skbdata, mi->metaid, 4, &edata);
125 EXPORT_SYMBOL_GPL(ife_encode_meta_u32);
127 int ife_get_meta_u16(struct sk_buff *skb, struct tcf_meta_info *mi)
130 return nla_put_u16(skb, mi->metaid, *(u16 *)mi->metaval);
132 return nla_put(skb, mi->metaid, 0, NULL);
134 EXPORT_SYMBOL_GPL(ife_get_meta_u16);
136 int ife_alloc_meta_u32(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
138 mi->metaval = kmemdup(metaval, sizeof(u32), gfp);
144 EXPORT_SYMBOL_GPL(ife_alloc_meta_u32);
146 int ife_alloc_meta_u16(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
148 mi->metaval = kmemdup(metaval, sizeof(u16), gfp);
154 EXPORT_SYMBOL_GPL(ife_alloc_meta_u16);
156 void ife_release_meta_gen(struct tcf_meta_info *mi)
160 EXPORT_SYMBOL_GPL(ife_release_meta_gen);
162 int ife_validate_meta_u32(void *val, int len)
164 if (len == sizeof(u32))
169 EXPORT_SYMBOL_GPL(ife_validate_meta_u32);
171 int ife_validate_meta_u16(void *val, int len)
173 /* length will not include padding */
174 if (len == sizeof(u16))
179 EXPORT_SYMBOL_GPL(ife_validate_meta_u16);
181 static LIST_HEAD(ifeoplist);
182 static DEFINE_RWLOCK(ife_mod_lock);
184 static struct tcf_meta_ops *find_ife_oplist(u16 metaid)
186 struct tcf_meta_ops *o;
188 read_lock(&ife_mod_lock);
189 list_for_each_entry(o, &ifeoplist, list) {
190 if (o->metaid == metaid) {
191 if (!try_module_get(o->owner))
193 read_unlock(&ife_mod_lock);
197 read_unlock(&ife_mod_lock);
202 int register_ife_op(struct tcf_meta_ops *mops)
204 struct tcf_meta_ops *m;
206 if (!mops->metaid || !mops->metatype || !mops->name ||
207 !mops->check_presence || !mops->encode || !mops->decode ||
208 !mops->get || !mops->alloc)
211 write_lock(&ife_mod_lock);
213 list_for_each_entry(m, &ifeoplist, list) {
214 if (m->metaid == mops->metaid ||
215 (strcmp(mops->name, m->name) == 0)) {
216 write_unlock(&ife_mod_lock);
222 mops->release = ife_release_meta_gen;
224 list_add_tail(&mops->list, &ifeoplist);
225 write_unlock(&ife_mod_lock);
228 EXPORT_SYMBOL_GPL(unregister_ife_op);
230 int unregister_ife_op(struct tcf_meta_ops *mops)
232 struct tcf_meta_ops *m;
235 write_lock(&ife_mod_lock);
236 list_for_each_entry(m, &ifeoplist, list) {
237 if (m->metaid == mops->metaid) {
238 list_del(&mops->list);
243 write_unlock(&ife_mod_lock);
247 EXPORT_SYMBOL_GPL(register_ife_op);
249 static int ife_validate_metatype(struct tcf_meta_ops *ops, void *val, int len)
252 /* XXX: unfortunately cant use nla_policy at this point
253 * because a length of 0 is valid in the case of
254 * "allow". "use" semantics do enforce for proper
255 * length and i couldve use nla_policy but it makes it hard
256 * to use it just for that..
259 return ops->validate(val, len);
261 if (ops->metatype == NLA_U32)
262 ret = ife_validate_meta_u32(val, len);
263 else if (ops->metatype == NLA_U16)
264 ret = ife_validate_meta_u16(val, len);
269 /* called when adding new meta information
270 * under ife->tcf_lock for existing action
272 static int load_metaops_and_vet(struct tcf_ife_info *ife, u32 metaid,
273 void *val, int len, bool exists)
275 struct tcf_meta_ops *ops = find_ife_oplist(metaid);
280 #ifdef CONFIG_MODULES
282 spin_unlock_bh(&ife->tcf_lock);
284 request_module("ifemeta%u", metaid);
287 spin_lock_bh(&ife->tcf_lock);
288 ops = find_ife_oplist(metaid);
295 ret = ife_validate_metatype(ops, val, len);
297 module_put(ops->owner);
303 /* called when adding new meta information
304 * under ife->tcf_lock for existing action
306 static int add_metainfo(struct tcf_ife_info *ife, u32 metaid, void *metaval,
307 int len, bool atomic)
309 struct tcf_meta_info *mi = NULL;
310 struct tcf_meta_ops *ops = find_ife_oplist(metaid);
316 mi = kzalloc(sizeof(*mi), atomic ? GFP_ATOMIC : GFP_KERNEL);
318 /*put back what find_ife_oplist took */
319 module_put(ops->owner);
326 ret = ops->alloc(mi, metaval, atomic ? GFP_ATOMIC : GFP_KERNEL);
329 module_put(ops->owner);
334 list_add_tail(&mi->metalist, &ife->metalist);
339 static int use_all_metadata(struct tcf_ife_info *ife)
341 struct tcf_meta_ops *o;
345 read_lock(&ife_mod_lock);
346 list_for_each_entry(o, &ifeoplist, list) {
347 rc = add_metainfo(ife, o->metaid, NULL, 0, true);
351 read_unlock(&ife_mod_lock);
359 static int dump_metalist(struct sk_buff *skb, struct tcf_ife_info *ife)
361 struct tcf_meta_info *e;
363 unsigned char *b = skb_tail_pointer(skb);
364 int total_encoded = 0;
366 /*can only happen on decode */
367 if (list_empty(&ife->metalist))
370 nest = nla_nest_start(skb, TCA_IFE_METALST);
374 list_for_each_entry(e, &ife->metalist, metalist) {
375 if (!e->ops->get(skb, e))
382 nla_nest_end(skb, nest);
391 /* under ife->tcf_lock */
392 static void _tcf_ife_cleanup(struct tc_action *a, int bind)
394 struct tcf_ife_info *ife = to_ife(a);
395 struct tcf_meta_info *e, *n;
397 list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
398 module_put(e->ops->owner);
399 list_del(&e->metalist);
410 static void tcf_ife_cleanup(struct tc_action *a, int bind)
412 struct tcf_ife_info *ife = to_ife(a);
414 spin_lock_bh(&ife->tcf_lock);
415 _tcf_ife_cleanup(a, bind);
416 spin_unlock_bh(&ife->tcf_lock);
419 /* under ife->tcf_lock for existing action */
420 static int populate_metalist(struct tcf_ife_info *ife, struct nlattr **tb,
428 for (i = 1; i < max_metacnt; i++) {
430 val = nla_data(tb[i]);
431 len = nla_len(tb[i]);
433 rc = load_metaops_and_vet(ife, i, val, len, exists);
437 rc = add_metainfo(ife, i, val, len, exists);
446 static int tcf_ife_init(struct net *net, struct nlattr *nla,
447 struct nlattr *est, struct tc_action **a,
450 struct tc_action_net *tn = net_generic(net, ife_net_id);
451 struct nlattr *tb[TCA_IFE_MAX + 1];
452 struct nlattr *tb2[IFE_META_MAX + 1];
453 struct tcf_ife_info *ife;
462 err = nla_parse_nested(tb, TCA_IFE_MAX, nla, ife_policy);
466 if (!tb[TCA_IFE_PARMS])
469 parm = nla_data(tb[TCA_IFE_PARMS]);
471 exists = tcf_hash_check(tn, parm->index, a, bind);
475 if (parm->flags & IFE_ENCODE) {
476 /* Until we get issued the ethertype, we cant have
479 if (!tb[TCA_IFE_TYPE]) {
481 tcf_hash_release(*a, bind);
482 pr_info("You MUST pass etherype for encoding\n");
488 ret = tcf_hash_create(tn, parm->index, est, a, &act_ife_ops,
494 tcf_hash_release(*a, bind);
500 ife->flags = parm->flags;
502 if (parm->flags & IFE_ENCODE) {
503 ife_type = nla_get_u16(tb[TCA_IFE_TYPE]);
504 if (tb[TCA_IFE_DMAC])
505 daddr = nla_data(tb[TCA_IFE_DMAC]);
506 if (tb[TCA_IFE_SMAC])
507 saddr = nla_data(tb[TCA_IFE_SMAC]);
511 spin_lock_bh(&ife->tcf_lock);
512 ife->tcf_action = parm->action;
514 if (parm->flags & IFE_ENCODE) {
516 ether_addr_copy(ife->eth_dst, daddr);
518 eth_zero_addr(ife->eth_dst);
521 ether_addr_copy(ife->eth_src, saddr);
523 eth_zero_addr(ife->eth_src);
525 ife->eth_type = ife_type;
528 if (ret == ACT_P_CREATED)
529 INIT_LIST_HEAD(&ife->metalist);
531 if (tb[TCA_IFE_METALST]) {
532 err = nla_parse_nested(tb2, IFE_META_MAX, tb[TCA_IFE_METALST],
537 tcf_hash_release(*a, bind);
538 if (ret == ACT_P_CREATED)
539 _tcf_ife_cleanup(*a, bind);
542 spin_unlock_bh(&ife->tcf_lock);
546 err = populate_metalist(ife, tb2, exists);
548 goto metadata_parse_err;
551 /* if no passed metadata allow list or passed allow-all
552 * then here we process by adding as many supported metadatum
553 * as we can. You better have at least one else we are
556 err = use_all_metadata(ife);
558 if (ret == ACT_P_CREATED)
559 _tcf_ife_cleanup(*a, bind);
562 spin_unlock_bh(&ife->tcf_lock);
568 spin_unlock_bh(&ife->tcf_lock);
570 if (ret == ACT_P_CREATED)
571 tcf_hash_insert(tn, *a);
576 static int tcf_ife_dump(struct sk_buff *skb, struct tc_action *a, int bind,
579 unsigned char *b = skb_tail_pointer(skb);
580 struct tcf_ife_info *ife = to_ife(a);
581 struct tc_ife opt = {
582 .index = ife->tcf_index,
583 .refcnt = ife->tcf_refcnt - ref,
584 .bindcnt = ife->tcf_bindcnt - bind,
585 .action = ife->tcf_action,
590 if (nla_put(skb, TCA_IFE_PARMS, sizeof(opt), &opt))
591 goto nla_put_failure;
593 tcf_tm_dump(&t, &ife->tcf_tm);
594 if (nla_put_64bit(skb, TCA_IFE_TM, sizeof(t), &t, TCA_IFE_PAD))
595 goto nla_put_failure;
597 if (!is_zero_ether_addr(ife->eth_dst)) {
598 if (nla_put(skb, TCA_IFE_DMAC, ETH_ALEN, ife->eth_dst))
599 goto nla_put_failure;
602 if (!is_zero_ether_addr(ife->eth_src)) {
603 if (nla_put(skb, TCA_IFE_SMAC, ETH_ALEN, ife->eth_src))
604 goto nla_put_failure;
607 if (nla_put(skb, TCA_IFE_TYPE, 2, &ife->eth_type))
608 goto nla_put_failure;
610 if (dump_metalist(skb, ife)) {
611 /*ignore failure to dump metalist */
612 pr_info("Failed to dump metalist\n");
622 int find_decode_metaid(struct sk_buff *skb, struct tcf_ife_info *ife,
623 u16 metaid, u16 mlen, void *mdata)
625 struct tcf_meta_info *e;
627 /* XXX: use hash to speed up */
628 list_for_each_entry(e, &ife->metalist, metalist) {
629 if (metaid == e->metaid) {
631 /* We check for decode presence already */
632 return e->ops->decode(skb, mdata, mlen);
650 static int tcf_ife_decode(struct sk_buff *skb, const struct tc_action *a,
651 struct tcf_result *res)
653 struct tcf_ife_info *ife = to_ife(a);
654 int action = ife->tcf_action;
655 struct ifeheadr *ifehdr = (struct ifeheadr *)skb->data;
656 int ifehdrln = (int)ifehdr->metalen;
657 struct meta_tlvhdr *tlv = (struct meta_tlvhdr *)(ifehdr->tlv_data);
659 spin_lock(&ife->tcf_lock);
660 bstats_update(&ife->tcf_bstats, skb);
661 tcf_lastuse_update(&ife->tcf_tm);
662 spin_unlock(&ife->tcf_lock);
664 ifehdrln = ntohs(ifehdrln);
665 if (unlikely(!pskb_may_pull(skb, ifehdrln))) {
666 spin_lock(&ife->tcf_lock);
667 ife->tcf_qstats.drops++;
668 spin_unlock(&ife->tcf_lock);
672 skb_set_mac_header(skb, ifehdrln);
673 __skb_pull(skb, ifehdrln);
674 skb->protocol = eth_type_trans(skb, skb->dev);
675 ifehdrln -= IFE_METAHDRLEN;
677 while (ifehdrln > 0) {
678 u8 *tlvdata = (u8 *)tlv;
679 u16 mtype = tlv->type;
683 mtype = ntohs(mtype);
685 alen = NLA_ALIGN(mlen);
687 if (find_decode_metaid(skb, ife, mtype, (mlen - NLA_HDRLEN),
688 (void *)(tlvdata + NLA_HDRLEN))) {
689 /* abuse overlimits to count when we receive metadata
690 * but dont have an ops for it
692 pr_info_ratelimited("Unknown metaid %d alnlen %d\n",
694 ife->tcf_qstats.overlimits++;
699 tlv = (struct meta_tlvhdr *)tlvdata;
702 skb_reset_network_header(skb);
706 /*XXX: check if we can do this at install time instead of current
709 static int ife_get_sz(struct sk_buff *skb, struct tcf_ife_info *ife)
711 struct tcf_meta_info *e, *n;
712 int tot_run_sz = 0, run_sz = 0;
714 list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
715 if (e->ops->check_presence) {
716 run_sz = e->ops->check_presence(skb, e);
717 tot_run_sz += run_sz;
724 static int tcf_ife_encode(struct sk_buff *skb, const struct tc_action *a,
725 struct tcf_result *res)
727 struct tcf_ife_info *ife = to_ife(a);
728 int action = ife->tcf_action;
729 struct ethhdr *oethh; /* outer ether header */
730 struct ethhdr *iethh; /* inner eth header */
731 struct tcf_meta_info *e;
733 OUTERHDR:TOTMETALEN:{TLVHDR:Metadatum:TLVHDR..}:ORIGDATA
734 where ORIGDATA = original ethernet header ...
736 u16 metalen = ife_get_sz(skb, ife);
737 int hdrm = metalen + skb->dev->hard_header_len + IFE_METAHDRLEN;
738 unsigned int skboff = skb->dev->hard_header_len;
739 int new_len = skb->len + hdrm;
740 bool exceed_mtu = false;
743 if (!skb_at_tc_ingress(skb)) {
744 if (new_len > skb->dev->mtu)
748 spin_lock(&ife->tcf_lock);
749 bstats_update(&ife->tcf_bstats, skb);
750 tcf_lastuse_update(&ife->tcf_tm);
752 if (!metalen) { /* no metadata to send */
753 /* abuse overlimits to count when we allow packet
756 ife->tcf_qstats.overlimits++;
757 spin_unlock(&ife->tcf_lock);
760 /* could be stupid policy setup or mtu config
761 * so lets be conservative.. */
762 if ((action == TC_ACT_SHOT) || exceed_mtu) {
763 ife->tcf_qstats.drops++;
764 spin_unlock(&ife->tcf_lock);
768 err = skb_cow_head(skb, hdrm);
770 ife->tcf_qstats.drops++;
771 spin_unlock(&ife->tcf_lock);
775 if (skb_at_tc_ingress(skb))
776 skb_push(skb, skb->dev->hard_header_len);
778 iethh = (struct ethhdr *)skb->data;
779 __skb_push(skb, hdrm);
780 memcpy(skb->data, iethh, skb->mac_len);
781 skb_reset_mac_header(skb);
782 oethh = eth_hdr(skb);
784 /*total metadata length */
785 metalen += IFE_METAHDRLEN;
786 metalen = htons(metalen);
787 memcpy((skb->data + skboff), &metalen, IFE_METAHDRLEN);
788 skboff += IFE_METAHDRLEN;
790 /* XXX: we dont have a clever way of telling encode to
791 * not repeat some of the computations that are done by
792 * ops->presence_check...
794 list_for_each_entry(e, &ife->metalist, metalist) {
795 if (e->ops->encode) {
796 err = e->ops->encode(skb, (void *)(skb->data + skboff),
800 /* too corrupt to keep around if overwritten */
801 ife->tcf_qstats.drops++;
802 spin_unlock(&ife->tcf_lock);
808 if (!is_zero_ether_addr(ife->eth_src))
809 ether_addr_copy(oethh->h_source, ife->eth_src);
811 ether_addr_copy(oethh->h_source, iethh->h_source);
812 if (!is_zero_ether_addr(ife->eth_dst))
813 ether_addr_copy(oethh->h_dest, ife->eth_dst);
815 ether_addr_copy(oethh->h_dest, iethh->h_dest);
816 oethh->h_proto = htons(ife->eth_type);
818 if (skb_at_tc_ingress(skb))
819 skb_pull(skb, skb->dev->hard_header_len);
821 spin_unlock(&ife->tcf_lock);
826 static int tcf_ife_act(struct sk_buff *skb, const struct tc_action *a,
827 struct tcf_result *res)
829 struct tcf_ife_info *ife = to_ife(a);
831 if (ife->flags & IFE_ENCODE)
832 return tcf_ife_encode(skb, a, res);
834 if (!(ife->flags & IFE_ENCODE))
835 return tcf_ife_decode(skb, a, res);
837 pr_info_ratelimited("unknown failure(policy neither de/encode\n");
838 spin_lock(&ife->tcf_lock);
839 bstats_update(&ife->tcf_bstats, skb);
840 tcf_lastuse_update(&ife->tcf_tm);
841 ife->tcf_qstats.drops++;
842 spin_unlock(&ife->tcf_lock);
847 static int tcf_ife_walker(struct net *net, struct sk_buff *skb,
848 struct netlink_callback *cb, int type,
849 const struct tc_action_ops *ops)
851 struct tc_action_net *tn = net_generic(net, ife_net_id);
853 return tcf_generic_walker(tn, skb, cb, type, ops);
856 static int tcf_ife_search(struct net *net, struct tc_action **a, u32 index)
858 struct tc_action_net *tn = net_generic(net, ife_net_id);
860 return tcf_hash_search(tn, a, index);
863 static struct tc_action_ops act_ife_ops = {
866 .owner = THIS_MODULE,
868 .dump = tcf_ife_dump,
869 .cleanup = tcf_ife_cleanup,
870 .init = tcf_ife_init,
871 .walk = tcf_ife_walker,
872 .lookup = tcf_ife_search,
873 .size = sizeof(struct tcf_ife_info),
876 static __net_init int ife_init_net(struct net *net)
878 struct tc_action_net *tn = net_generic(net, ife_net_id);
880 return tc_action_net_init(tn, &act_ife_ops, IFE_TAB_MASK);
883 static void __net_exit ife_exit_net(struct net *net)
885 struct tc_action_net *tn = net_generic(net, ife_net_id);
887 tc_action_net_exit(tn);
890 static struct pernet_operations ife_net_ops = {
891 .init = ife_init_net,
892 .exit = ife_exit_net,
894 .size = sizeof(struct tc_action_net),
897 static int __init ife_init_module(void)
899 return tcf_register_action(&act_ife_ops, &ife_net_ops);
902 static void __exit ife_cleanup_module(void)
904 tcf_unregister_action(&act_ife_ops, &ife_net_ops);
907 module_init(ife_init_module);
908 module_exit(ife_cleanup_module);
910 MODULE_AUTHOR("Jamal Hadi Salim(2015)");
911 MODULE_DESCRIPTION("Inter-FE LFB action");
912 MODULE_LICENSE("GPL");