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 int ife_net_id;
39 static int max_metacnt = IFE_META_MAX + 1;
41 static const struct nla_policy ife_policy[TCA_IFE_MAX + 1] = {
42 [TCA_IFE_PARMS] = { .len = sizeof(struct tc_ife)},
43 [TCA_IFE_DMAC] = { .len = ETH_ALEN},
44 [TCA_IFE_SMAC] = { .len = ETH_ALEN},
45 [TCA_IFE_TYPE] = { .type = NLA_U16},
48 /* Caller takes care of presenting data in network order
50 int ife_tlv_meta_encode(void *skbdata, u16 attrtype, u16 dlen, const void *dval)
52 u32 *tlv = (u32 *)(skbdata);
53 u16 totlen = nla_total_size(dlen); /*alignment + hdr */
54 char *dptr = (char *)tlv + NLA_HDRLEN;
55 u32 htlv = attrtype << 16 | totlen;
58 memset(dptr, 0, totlen - NLA_HDRLEN);
59 memcpy(dptr, dval, dlen);
63 EXPORT_SYMBOL_GPL(ife_tlv_meta_encode);
65 int ife_get_meta_u32(struct sk_buff *skb, struct tcf_meta_info *mi)
68 return nla_put_u32(skb, mi->metaid, *(u32 *)mi->metaval);
70 return nla_put(skb, mi->metaid, 0, NULL);
72 EXPORT_SYMBOL_GPL(ife_get_meta_u32);
74 int ife_check_meta_u32(u32 metaval, struct tcf_meta_info *mi)
76 if (metaval || mi->metaval)
77 return 8; /* T+L+V == 2+2+4 */
81 EXPORT_SYMBOL_GPL(ife_check_meta_u32);
83 int ife_encode_meta_u32(u32 metaval, void *skbdata, struct tcf_meta_info *mi)
88 edata = *(u32 *)mi->metaval;
92 if (!edata) /* will not encode */
96 return ife_tlv_meta_encode(skbdata, mi->metaid, 4, &edata);
98 EXPORT_SYMBOL_GPL(ife_encode_meta_u32);
100 int ife_get_meta_u16(struct sk_buff *skb, struct tcf_meta_info *mi)
103 return nla_put_u16(skb, mi->metaid, *(u16 *)mi->metaval);
105 return nla_put(skb, mi->metaid, 0, NULL);
107 EXPORT_SYMBOL_GPL(ife_get_meta_u16);
109 int ife_alloc_meta_u32(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
111 mi->metaval = kmemdup(metaval, sizeof(u32), gfp);
117 EXPORT_SYMBOL_GPL(ife_alloc_meta_u32);
119 int ife_alloc_meta_u16(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
121 mi->metaval = kmemdup(metaval, sizeof(u16), gfp);
127 EXPORT_SYMBOL_GPL(ife_alloc_meta_u16);
129 void ife_release_meta_gen(struct tcf_meta_info *mi)
133 EXPORT_SYMBOL_GPL(ife_release_meta_gen);
135 int ife_validate_meta_u32(void *val, int len)
142 EXPORT_SYMBOL_GPL(ife_validate_meta_u32);
144 int ife_validate_meta_u16(void *val, int len)
146 /* length will include padding */
147 if (len == NLA_ALIGN(2))
152 EXPORT_SYMBOL_GPL(ife_validate_meta_u16);
154 static LIST_HEAD(ifeoplist);
155 static DEFINE_RWLOCK(ife_mod_lock);
157 static struct tcf_meta_ops *find_ife_oplist(u16 metaid)
159 struct tcf_meta_ops *o;
161 read_lock(&ife_mod_lock);
162 list_for_each_entry(o, &ifeoplist, list) {
163 if (o->metaid == metaid) {
164 if (!try_module_get(o->owner))
166 read_unlock(&ife_mod_lock);
170 read_unlock(&ife_mod_lock);
175 int register_ife_op(struct tcf_meta_ops *mops)
177 struct tcf_meta_ops *m;
179 if (!mops->metaid || !mops->metatype || !mops->name ||
180 !mops->check_presence || !mops->encode || !mops->decode ||
181 !mops->get || !mops->alloc)
184 write_lock(&ife_mod_lock);
186 list_for_each_entry(m, &ifeoplist, list) {
187 if (m->metaid == mops->metaid ||
188 (strcmp(mops->name, m->name) == 0)) {
189 write_unlock(&ife_mod_lock);
195 mops->release = ife_release_meta_gen;
197 list_add_tail(&mops->list, &ifeoplist);
198 write_unlock(&ife_mod_lock);
201 EXPORT_SYMBOL_GPL(unregister_ife_op);
203 int unregister_ife_op(struct tcf_meta_ops *mops)
205 struct tcf_meta_ops *m;
208 write_lock(&ife_mod_lock);
209 list_for_each_entry(m, &ifeoplist, list) {
210 if (m->metaid == mops->metaid) {
211 list_del(&mops->list);
216 write_unlock(&ife_mod_lock);
220 EXPORT_SYMBOL_GPL(register_ife_op);
222 static int ife_validate_metatype(struct tcf_meta_ops *ops, void *val, int len)
225 /* XXX: unfortunately cant use nla_policy at this point
226 * because a length of 0 is valid in the case of
227 * "allow". "use" semantics do enforce for proper
228 * length and i couldve use nla_policy but it makes it hard
229 * to use it just for that..
232 return ops->validate(val, len);
234 if (ops->metatype == NLA_U32)
235 ret = ife_validate_meta_u32(val, len);
236 else if (ops->metatype == NLA_U16)
237 ret = ife_validate_meta_u16(val, len);
242 /* called when adding new meta information
243 * under ife->tcf_lock for existing action
245 static int load_metaops_and_vet(struct tcf_ife_info *ife, u32 metaid,
246 void *val, int len, bool exists)
248 struct tcf_meta_ops *ops = find_ife_oplist(metaid);
253 #ifdef CONFIG_MODULES
255 spin_unlock_bh(&ife->tcf_lock);
257 request_module("ifemeta%u", metaid);
260 spin_lock_bh(&ife->tcf_lock);
261 ops = find_ife_oplist(metaid);
268 ret = ife_validate_metatype(ops, val, len);
270 module_put(ops->owner);
276 /* called when adding new meta information
277 * under ife->tcf_lock for existing action
279 static int add_metainfo(struct tcf_ife_info *ife, u32 metaid, void *metaval,
280 int len, bool atomic)
282 struct tcf_meta_info *mi = NULL;
283 struct tcf_meta_ops *ops = find_ife_oplist(metaid);
289 mi = kzalloc(sizeof(*mi), atomic ? GFP_ATOMIC : GFP_KERNEL);
291 /*put back what find_ife_oplist took */
292 module_put(ops->owner);
299 ret = ops->alloc(mi, metaval, atomic ? GFP_ATOMIC : GFP_KERNEL);
302 module_put(ops->owner);
307 list_add_tail(&mi->metalist, &ife->metalist);
312 static int use_all_metadata(struct tcf_ife_info *ife)
314 struct tcf_meta_ops *o;
318 read_lock(&ife_mod_lock);
319 list_for_each_entry(o, &ifeoplist, list) {
320 rc = add_metainfo(ife, o->metaid, NULL, 0, true);
324 read_unlock(&ife_mod_lock);
332 static int dump_metalist(struct sk_buff *skb, struct tcf_ife_info *ife)
334 struct tcf_meta_info *e;
336 unsigned char *b = skb_tail_pointer(skb);
337 int total_encoded = 0;
339 /*can only happen on decode */
340 if (list_empty(&ife->metalist))
343 nest = nla_nest_start(skb, TCA_IFE_METALST);
347 list_for_each_entry(e, &ife->metalist, metalist) {
348 if (!e->ops->get(skb, e))
355 nla_nest_end(skb, nest);
364 /* under ife->tcf_lock */
365 static void _tcf_ife_cleanup(struct tc_action *a, int bind)
367 struct tcf_ife_info *ife = a->priv;
368 struct tcf_meta_info *e, *n;
370 list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
371 module_put(e->ops->owner);
372 list_del(&e->metalist);
383 static void tcf_ife_cleanup(struct tc_action *a, int bind)
385 struct tcf_ife_info *ife = a->priv;
387 spin_lock_bh(&ife->tcf_lock);
388 _tcf_ife_cleanup(a, bind);
389 spin_unlock_bh(&ife->tcf_lock);
392 /* under ife->tcf_lock for existing action */
393 static int populate_metalist(struct tcf_ife_info *ife, struct nlattr **tb,
401 for (i = 1; i < max_metacnt; i++) {
403 val = nla_data(tb[i]);
404 len = nla_len(tb[i]);
406 rc = load_metaops_and_vet(ife, i, val, len, exists);
410 rc = add_metainfo(ife, i, val, len, exists);
419 static int tcf_ife_init(struct net *net, struct nlattr *nla,
420 struct nlattr *est, struct tc_action *a,
423 struct tc_action_net *tn = net_generic(net, ife_net_id);
424 struct nlattr *tb[TCA_IFE_MAX + 1];
425 struct nlattr *tb2[IFE_META_MAX + 1];
426 struct tcf_ife_info *ife;
435 err = nla_parse_nested(tb, TCA_IFE_MAX, nla, ife_policy);
439 if (!tb[TCA_IFE_PARMS])
442 parm = nla_data(tb[TCA_IFE_PARMS]);
444 exists = tcf_hash_check(tn, parm->index, a, bind);
448 if (parm->flags & IFE_ENCODE) {
449 /* Until we get issued the ethertype, we cant have
452 if (!tb[TCA_IFE_TYPE]) {
454 tcf_hash_release(a, bind);
455 pr_info("You MUST pass etherype for encoding\n");
461 ret = tcf_hash_create(tn, parm->index, est, a, sizeof(*ife),
467 tcf_hash_release(a, bind);
473 ife->flags = parm->flags;
475 if (parm->flags & IFE_ENCODE) {
476 ife_type = nla_get_u16(tb[TCA_IFE_TYPE]);
477 if (tb[TCA_IFE_DMAC])
478 daddr = nla_data(tb[TCA_IFE_DMAC]);
479 if (tb[TCA_IFE_SMAC])
480 saddr = nla_data(tb[TCA_IFE_SMAC]);
484 spin_lock_bh(&ife->tcf_lock);
485 ife->tcf_action = parm->action;
487 if (parm->flags & IFE_ENCODE) {
489 ether_addr_copy(ife->eth_dst, daddr);
491 eth_zero_addr(ife->eth_dst);
494 ether_addr_copy(ife->eth_src, saddr);
496 eth_zero_addr(ife->eth_src);
498 ife->eth_type = ife_type;
501 if (ret == ACT_P_CREATED)
502 INIT_LIST_HEAD(&ife->metalist);
504 if (tb[TCA_IFE_METALST]) {
505 err = nla_parse_nested(tb2, IFE_META_MAX, tb[TCA_IFE_METALST],
510 tcf_hash_release(a, bind);
511 if (ret == ACT_P_CREATED)
512 _tcf_ife_cleanup(a, bind);
515 spin_unlock_bh(&ife->tcf_lock);
519 err = populate_metalist(ife, tb2, exists);
521 goto metadata_parse_err;
524 /* if no passed metadata allow list or passed allow-all
525 * then here we process by adding as many supported metadatum
526 * as we can. You better have at least one else we are
529 err = use_all_metadata(ife);
531 if (ret == ACT_P_CREATED)
532 _tcf_ife_cleanup(a, bind);
535 spin_unlock_bh(&ife->tcf_lock);
541 spin_unlock_bh(&ife->tcf_lock);
543 if (ret == ACT_P_CREATED)
544 tcf_hash_insert(tn, a);
549 static int tcf_ife_dump(struct sk_buff *skb, struct tc_action *a, int bind,
552 unsigned char *b = skb_tail_pointer(skb);
553 struct tcf_ife_info *ife = a->priv;
554 struct tc_ife opt = {
555 .index = ife->tcf_index,
556 .refcnt = ife->tcf_refcnt - ref,
557 .bindcnt = ife->tcf_bindcnt - bind,
558 .action = ife->tcf_action,
563 if (nla_put(skb, TCA_IFE_PARMS, sizeof(opt), &opt))
564 goto nla_put_failure;
566 tcf_tm_dump(&t, &ife->tcf_tm);
567 if (nla_put_64bit(skb, TCA_IFE_TM, sizeof(t), &t, TCA_IFE_PAD))
568 goto nla_put_failure;
570 if (!is_zero_ether_addr(ife->eth_dst)) {
571 if (nla_put(skb, TCA_IFE_DMAC, ETH_ALEN, ife->eth_dst))
572 goto nla_put_failure;
575 if (!is_zero_ether_addr(ife->eth_src)) {
576 if (nla_put(skb, TCA_IFE_SMAC, ETH_ALEN, ife->eth_src))
577 goto nla_put_failure;
580 if (nla_put(skb, TCA_IFE_TYPE, 2, &ife->eth_type))
581 goto nla_put_failure;
583 if (dump_metalist(skb, ife)) {
584 /*ignore failure to dump metalist */
585 pr_info("Failed to dump metalist\n");
595 int find_decode_metaid(struct sk_buff *skb, struct tcf_ife_info *ife,
596 u16 metaid, u16 mlen, void *mdata)
598 struct tcf_meta_info *e;
600 /* XXX: use hash to speed up */
601 list_for_each_entry(e, &ife->metalist, metalist) {
602 if (metaid == e->metaid) {
604 /* We check for decode presence already */
605 return e->ops->decode(skb, mdata, mlen);
623 static int tcf_ife_decode(struct sk_buff *skb, const struct tc_action *a,
624 struct tcf_result *res)
626 struct tcf_ife_info *ife = a->priv;
627 int action = ife->tcf_action;
628 struct ifeheadr *ifehdr = (struct ifeheadr *)skb->data;
629 u16 ifehdrln = ifehdr->metalen;
630 struct meta_tlvhdr *tlv = (struct meta_tlvhdr *)(ifehdr->tlv_data);
632 spin_lock(&ife->tcf_lock);
633 bstats_update(&ife->tcf_bstats, skb);
634 tcf_lastuse_update(&ife->tcf_tm);
635 spin_unlock(&ife->tcf_lock);
637 ifehdrln = ntohs(ifehdrln);
638 if (unlikely(!pskb_may_pull(skb, ifehdrln))) {
639 spin_lock(&ife->tcf_lock);
640 ife->tcf_qstats.drops++;
641 spin_unlock(&ife->tcf_lock);
645 skb_set_mac_header(skb, ifehdrln);
646 __skb_pull(skb, ifehdrln);
647 skb->protocol = eth_type_trans(skb, skb->dev);
648 ifehdrln -= IFE_METAHDRLEN;
650 while (ifehdrln > 0) {
651 u8 *tlvdata = (u8 *)tlv;
652 u16 mtype = tlv->type;
655 mtype = ntohs(mtype);
658 if (find_decode_metaid(skb, ife, mtype, (mlen - 4),
659 (void *)(tlvdata + 4))) {
660 /* abuse overlimits to count when we receive metadata
661 * but dont have an ops for it
663 pr_info_ratelimited("Unknown metaid %d alnlen %d\n",
665 ife->tcf_qstats.overlimits++;
670 tlv = (struct meta_tlvhdr *)tlvdata;
673 skb_reset_network_header(skb);
677 /*XXX: check if we can do this at install time instead of current
680 static int ife_get_sz(struct sk_buff *skb, struct tcf_ife_info *ife)
682 struct tcf_meta_info *e, *n;
683 int tot_run_sz = 0, run_sz = 0;
685 list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
686 if (e->ops->check_presence) {
687 run_sz = e->ops->check_presence(skb, e);
688 tot_run_sz += run_sz;
695 static int tcf_ife_encode(struct sk_buff *skb, const struct tc_action *a,
696 struct tcf_result *res)
698 struct tcf_ife_info *ife = a->priv;
699 int action = ife->tcf_action;
700 struct ethhdr *oethh; /* outer ether header */
701 struct ethhdr *iethh; /* inner eth header */
702 struct tcf_meta_info *e;
704 OUTERHDR:TOTMETALEN:{TLVHDR:Metadatum:TLVHDR..}:ORIGDATA
705 where ORIGDATA = original ethernet header ...
707 u16 metalen = ife_get_sz(skb, ife);
708 int hdrm = metalen + skb->dev->hard_header_len + IFE_METAHDRLEN;
709 unsigned int skboff = skb->dev->hard_header_len;
710 u32 at = G_TC_AT(skb->tc_verd);
711 int new_len = skb->len + hdrm;
712 bool exceed_mtu = false;
715 if (at & AT_EGRESS) {
716 if (new_len > skb->dev->mtu)
720 spin_lock(&ife->tcf_lock);
721 bstats_update(&ife->tcf_bstats, skb);
722 tcf_lastuse_update(&ife->tcf_tm);
724 if (!metalen) { /* no metadata to send */
725 /* abuse overlimits to count when we allow packet
728 ife->tcf_qstats.overlimits++;
729 spin_unlock(&ife->tcf_lock);
732 /* could be stupid policy setup or mtu config
733 * so lets be conservative.. */
734 if ((action == TC_ACT_SHOT) || exceed_mtu) {
735 ife->tcf_qstats.drops++;
736 spin_unlock(&ife->tcf_lock);
740 iethh = eth_hdr(skb);
742 err = skb_cow_head(skb, hdrm);
744 ife->tcf_qstats.drops++;
745 spin_unlock(&ife->tcf_lock);
749 if (!(at & AT_EGRESS))
750 skb_push(skb, skb->dev->hard_header_len);
752 __skb_push(skb, hdrm);
753 memcpy(skb->data, iethh, skb->mac_len);
754 skb_reset_mac_header(skb);
755 oethh = eth_hdr(skb);
757 /*total metadata length */
758 metalen += IFE_METAHDRLEN;
759 metalen = htons(metalen);
760 memcpy((skb->data + skboff), &metalen, IFE_METAHDRLEN);
761 skboff += IFE_METAHDRLEN;
763 /* XXX: we dont have a clever way of telling encode to
764 * not repeat some of the computations that are done by
765 * ops->presence_check...
767 list_for_each_entry(e, &ife->metalist, metalist) {
768 if (e->ops->encode) {
769 err = e->ops->encode(skb, (void *)(skb->data + skboff),
773 /* too corrupt to keep around if overwritten */
774 ife->tcf_qstats.drops++;
775 spin_unlock(&ife->tcf_lock);
781 if (!is_zero_ether_addr(ife->eth_src))
782 ether_addr_copy(oethh->h_source, ife->eth_src);
784 ether_addr_copy(oethh->h_source, iethh->h_source);
785 if (!is_zero_ether_addr(ife->eth_dst))
786 ether_addr_copy(oethh->h_dest, ife->eth_dst);
788 ether_addr_copy(oethh->h_dest, iethh->h_dest);
789 oethh->h_proto = htons(ife->eth_type);
791 if (!(at & AT_EGRESS))
792 skb_pull(skb, skb->dev->hard_header_len);
794 spin_unlock(&ife->tcf_lock);
799 static int tcf_ife_act(struct sk_buff *skb, const struct tc_action *a,
800 struct tcf_result *res)
802 struct tcf_ife_info *ife = a->priv;
804 if (ife->flags & IFE_ENCODE)
805 return tcf_ife_encode(skb, a, res);
807 if (!(ife->flags & IFE_ENCODE))
808 return tcf_ife_decode(skb, a, res);
810 pr_info_ratelimited("unknown failure(policy neither de/encode\n");
811 spin_lock(&ife->tcf_lock);
812 bstats_update(&ife->tcf_bstats, skb);
813 tcf_lastuse_update(&ife->tcf_tm);
814 ife->tcf_qstats.drops++;
815 spin_unlock(&ife->tcf_lock);
820 static int tcf_ife_walker(struct net *net, struct sk_buff *skb,
821 struct netlink_callback *cb, int type,
824 struct tc_action_net *tn = net_generic(net, ife_net_id);
826 return tcf_generic_walker(tn, skb, cb, type, a);
829 static int tcf_ife_search(struct net *net, struct tc_action *a, u32 index)
831 struct tc_action_net *tn = net_generic(net, ife_net_id);
833 return tcf_hash_search(tn, a, index);
836 static struct tc_action_ops act_ife_ops = {
839 .owner = THIS_MODULE,
841 .dump = tcf_ife_dump,
842 .cleanup = tcf_ife_cleanup,
843 .init = tcf_ife_init,
844 .walk = tcf_ife_walker,
845 .lookup = tcf_ife_search,
848 static __net_init int ife_init_net(struct net *net)
850 struct tc_action_net *tn = net_generic(net, ife_net_id);
852 return tc_action_net_init(tn, &act_ife_ops, IFE_TAB_MASK);
855 static void __net_exit ife_exit_net(struct net *net)
857 struct tc_action_net *tn = net_generic(net, ife_net_id);
859 tc_action_net_exit(tn);
862 static struct pernet_operations ife_net_ops = {
863 .init = ife_init_net,
864 .exit = ife_exit_net,
866 .size = sizeof(struct tc_action_net),
869 static int __init ife_init_module(void)
871 return tcf_register_action(&act_ife_ops, &ife_net_ops);
874 static void __exit ife_cleanup_module(void)
876 tcf_unregister_action(&act_ife_ops, &ife_net_ops);
879 module_init(ife_init_module);
880 module_exit(ife_cleanup_module);
882 MODULE_AUTHOR("Jamal Hadi Salim(2015)");
883 MODULE_DESCRIPTION("Inter-FE LFB action");
884 MODULE_LICENSE("GPL");